-1

This code takes the coordinates of two rectangles and finds their intersection.

def rec_intersection(rect1, rect2)
  x_min = [rect1[0][0], rect2[0][1]].max
  x_max = [rect1[1][0], rect2[1][1]].min
  y_min = [rect1[0][0], rect2[0][1]].max
  y_max = [rect1[1][0], rect2[1][1]].min
  return nil if ((x_max < x_min) || (y_max < y_min))
  return [[x_min, y_min], [x_max, y_max]]
end

rec_intersection([[1, 1], [2, 2]],[[0, 0], [5, 5]])

I don't really understand it. Specifically I would like to know more about exactly what the coordinates mean (I know they are coordinates for the left-bottom and right-top) but can anybody elaborate more? What are they relative to? The size of the rectangle? Or it's placement? How does a rectangle with a bottom-left coordinate of [1,1] differ from one with a bottom-left of [0,0]?

Also I would like to know why in order to find the x_min, the max method is being used (and vice-versa). Any clarification is appreciated.

HolyMoly
  • 2,020
  • 3
  • 22
  • 35
  • 1
    Your worship, rectangles are commonly specified by giving the coordinates for two opposite corners. In your example, `rect1 = [[1,1],[2,2]]`, so `[1,1]` would be the coordinate for the top-left corner and `[2,2]` would be the coordinate in the bottom-right corner. The other two corners therefore would be at `[1,2]` (top-right) and `[2,1]` (bottom-left). – Cary Swoveland Mar 20 '15 at 00:42
  • so in the second rectangle the top-left coordinates are [0,0] compared to the first rectangle's top-left of [1,1] ...so what does that tell me about the rectangles? that they are different in size, or location?... since they are both top-lefts – HolyMoly Mar 20 '15 at 01:04
  • 1
    @HolyMoly both. It sounds like maybe you should spend some time catching up with Pythagoras or Descartes. – Iron Savior Mar 20 '15 at 01:25

1 Answers1

1

This is a comment, which I will delete after the OP has seen it. Here's a graph of the two rectangles, where rect1 is contained within rect2.

enter image description here

Earlier, where I referred to [1,1] and [2,2] as the "top-left" and "bottom-right" corners, respectively, of rect1, that should have been the "bottom-left" and "top-right" corners.

Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100