Confused about rectangles in ruby

萝らか妹 提交于 2019-12-12 06:47:34

问题


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.


回答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.

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.



来源:https://stackoverflow.com/questions/29157419/confused-about-rectangles-in-ruby

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!