对 ruby 中的矩形感到困惑 [英] Confused about rectangles in ruby
问题描述
此代码获取两个矩形的坐标并找到它们的交点.
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]])
我真的不明白.具体来说,我想更多地了解坐标的确切含义(我知道它们是左下角和右上角的坐标),但有人可以详细说明吗?它们与什么有关?长方形的大小?还是定位?左下角坐标为 [1,1] 的矩形与左下角坐标为 [0,0] 的矩形有何不同?
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]?
我还想知道为什么为了找到 x_min,使用了 max 方法(反之亦然).任何澄清表示赞赏.
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.
推荐答案
这是一条评论,我会在 OP 看到后删除.这是两个矩形的图形,其中 rect1
包含在 rect2
中.
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
.
之前,我将 [1,1]
和 [2,2]
分别称为左上角"和右下角",rect1
,应该是左下角"和右上角"角.
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.
这篇关于对 ruby 中的矩形感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!