Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rectangle intersection in Ruby

I'm trying to understand this program, but I'm having some difficulty. I don't understand the part with x_min, y_min, x_max, y_max.

I understand the program passes through two rectangles with the bottom-left and top-right coordinate points, but where do the array indices [0][0],[1][1], etc. come from?

I'm confused about what's happening, so an explanation would help.

# Write a function, `rec_intersection(rect1, rect2)` and returns the
# intersection of the two.
#
# Rectangles are represented as a pair of coordinate-pairs: the
# bottom-left and top-right coordinates (given in `[x, y]` notation).
#
# Hint: You can calculate the left-most x coordinate of the
# intersection by taking the maximum of the left-most x coordinate of
# each rectangle. Likewise, you can calculate the top-most y
# coordinate of the intersection by taking the minimum of the top most
# y coordinate of each rectangle.
#
# Difficulty: 4/5
def rec_intersection(rect1, rect2)

x_min = [rect1[0][0], rect2[0][0]].max
x_max = [rect1[1][0], rect2[1][0]].min

y_min = [rect1[0][1], rect2[0][1]].max
y_max = [rect1[1][1], 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

puts rec_intersection(
      [[0, 0], [2, 1]],
      [[1, 0], [3, 1]]
    ) == [[1, 0], [2, 1]]

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


puts rec_intersection(
      [[1, 1], [2, 2]],
      [[4, 4], [5, 5]]
    ) == nil

puts rec_intersection(
      [[1, 1], [5, 4]],
      [[2, 2], [3, 5]]
    ) == [[2, 2], [3, 4]]
like image 684
ceckenrode Avatar asked Dec 19 '22 01:12

ceckenrode


2 Answers

The variables x_min, x_max, y_min, y_max are used to store the coordinates of the intersecting region. They are obtained using max and min on a two-value array using the passed-in rectangles. Calling [1 ,2].max will return 2, and calling [1,2].min will return 1 for example.

The reason why these variables represent the intersecting rectangle is probably easier to understand through an image (extremely detailed and professional diagram incoming): rectangle intersect for dummies

As you can see, the minimum value of the yellow (intersecting) rectangle can be no less than the minimum value of the red rectangle. The max value can be no less than the blue rectangle's max value.

like image 195
Gnarlywhale Avatar answered Dec 21 '22 15:12

Gnarlywhale


What I don't get in particular is the part with x_min, y_min, x_max, y_max. I get the the program passes through 2 rectangles with the bottom left and top right coordinate points. But where do the array indices come from? [0][0] , [1][1], etc?

This section of the comments above that code is important to understand this:

# Rectangles are represented as a pair of coordinate-pairs: the
# bottom-left and top-right coordinates (given in `[x, y]` notation).

So, if rect is a rectangle, then rect[0] represents the bottom-left corner, and rect[1] represents the top-right corner. Further, rect[0][0] represents the x-coordinate of the bottom-left corner, rect[0][1] is the y-coordinate of that corner, and so on.

This section of the comments is also important:

# Hint: You can calculate the left-most x coordinate of the
# intersection by taking the maximum of the left-most x coordinate of
# each rectangle. [...]

If rect is a rectangle, the left-most x-coordinate of that rectangle is the x-coordinate of the bottom-left corner. As I explained above, rect[0][0] represents the x-coordinate of the bottom-left corner. So, in this line:

x_min = [rect1[0][0], rect2[0][0]].max

rect1[0][0] and rect2[0][0] are the two leftmost x-coordinates of the rectangles, and this line of code is saying that the x-coordinate of the leftmost side of the intersection of the two rectangles is equal to whichever one of these is bigger.

like image 33
Adrian Avatar answered Dec 21 '22 15:12

Adrian