Rectangular Intersection in Ruby

I am trying to understand this program, but I have some difficulties. I do not understand the part with x_min , y_min , x_max , y_max .

I understand that the program goes through two rectangles with lower left and upper right coordinates, but where are the array indices [0][0] , [1][1] , etc. come from?

I am confused by what is happening, so an explanation will 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]] 
+4
source share
3 answers

What I am not getting in particular is the part with x_min, y_min, x_max, y_max. I get the program to go through 2 rectangles with lower left and upper right coordinate points. But where do the array indices come from? [0] [0], [1] [1], etc.

This comments section above this code is important for understanding 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 lower left corner, and rect[1] represents the upper right corner. In addition, rect[0][0] represents the x-coordinate of the lower left corner, rect[0][1] is the y-coordinate of this angle, etc.

This comment section 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 leftmost x-coordinate of this rectangle is the x-coordinate of the lower left corner. As I explained above, rect[0][0] represents the x coordinate of the lower 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 indicates that the x-coordinate of the left-most side of the intersection of the two rectangles is equal to any of them larger.

+3
source

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 in a two-digit array using rectangles with a transmission. A call to [1 ,2].max will return 2 , and a call to [1,2].min will return, for example, 1 .

The reason these variables are an intersecting rectangle is perhaps easier to understand through the image (an extremely detailed and professional diagram is included): rectangle intersects for mannequins

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 maximum value can be no less than the maximum value of the blue rectangle.

+8
source

Basically, when he looks for the maximum x_min or min x_max , he first asks "what is the array of x values", then asks "what value".

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

specifically searches for [rect1 [first array (0)] [first value (0)]

0
source

All Articles