如何计算两个矩形之间的距离?(背景:Lua的游戏.)

Phi*_*sen 18 lua distance rectangles

给定两个具有x,y,宽度,高度(以像素为单位)的矩形以及以度为单位的旋转值 - 如何计算其轮廓彼此的最近距离?

背景:在用Lua编写的游戏中,我随机生成地图,但是想要确保某些矩形彼此不太靠近 - 这是必要的,因为如果矩形进入某个近距离位置,地图变得无法解决,如球需要在他们之间传球.速度不是一个大问题,因为我没有很多矩形,而且每个级别只生成一次地图.我在StackOverflow上找到的以前的链接是这个这个

提前谢谢了!

Max*_*xim 24

不是Lua,一个基于M Katz建议的Python代码:

def rect_distance((x1, y1, x1b, y1b), (x2, y2, x2b, y2b)):
    left = x2b < x1
    right = x1b < x2
    bottom = y2b < y1
    top = y1b < y2
    if top and left:
        return dist((x1, y1b), (x2b, y2))
    elif left and bottom:
        return dist((x1, y1), (x2b, y2b))
    elif bottom and right:
        return dist((x1b, y1), (x2, y2b))
    elif right and top:
        return dist((x1b, y1b), (x2, y2))
    elif left:
        return x1 - x2b
    elif right:
        return x2 - x1b
    elif bottom:
        return y1 - y2b
    elif top:
        return y2 - y1b
    else:             # rectangles intersect
        return 0.
Run Code Online (Sandbox Code Playgroud)

哪里

  • dist 是点之间的欧氏距离
  • RECT.1由点(x1, y1)和形成(x1b, y1b)
  • RECT.2由点(x2, y2)和形成(x2b, y2b)

  • 该解决方案不考虑矩形的旋转。假设矩形左上角有一个点,第二个点是倾斜的,那么计算的第二个点不应该是其中一个角。 (3认同)

M K*_*atz 12

Edit: As OK points out, this solution assumes all the rectangles are upright. To make it work for rotated rectangles as the OP asks you'd also have to compute the distance from the corners of each rectangle to the closest side of the other rectangle. But you can avoid doing that computation in most cases if the point is above or below both end points of the line segment, and to the left or right of both line segments (in telephone positions 1, 3, 7, or 9 with respect to the line segment).

Agnius's answer relies on a DistanceBetweenLineSegments() function. Here is a case analysis that does not:

(1) Check if the rects intersect. If so, the distance between them is 0.
(2) If not, think of r2 as the center of a telephone key pad, #5.
(3) r1 may be fully in one of the extreme quadrants (#1, #3, #7, or #9). If so
    the distance is the distance from one rect corner to another (e.g., if r1 is
    in quadrant #1, the distance is the distance from the lower-right corner of
    r1 to the upper-left corner of r2).
(4) Otherwise r1 is to the left, right, above, or below r2 and the distance is
    the distance between the relevant sides (e.g., if r1 is above, the distance
    is the distance between r1's low y and r2's high y).
Run Code Online (Sandbox Code Playgroud)


Fel*_* K. 5

实际上有一个快速的数学解决方案。

Length(Max((0, 0), Abs(Center - otherCenter) - (Extent + otherExtent)))
Run Code Online (Sandbox Code Playgroud)

哪里Center = ((Maximum - Minimum) / 2) + MinimumExtent = (Maximum - Minimum) / 2。基本上,零轴上方的代码是重叠的,因此距离始终是正确的。

最好将矩形保留为这种格式,因为它在许多情况下更可取(ae 旋转更容易)。


Roy*_* T. 0

这个问题取决于什么样的距离。您想要中心距离、边缘距离还是最近角点距离?

我猜你指的是最后一个。如果 X 和 Y 值指示矩形的中心,那么您可以通过应用此技巧找到每个角

//Pseudo code
Vector2 BottomLeftCorner = new Vector2(width / 2, heigth / 2);
BottomLeftCorner = BottomLeftCorner * Matrix.CreateRotation(MathHelper.ToRadians(degrees));
//If LUA has no built in Vector/Matrix calculus search for "rotate Vector" on the web.
//this helps: http://www.kirupa.com/forum/archive/index.php/t-12181.html

BottomLeftCorner += new Vector2(X, Y); //add the origin so that we have to world position.
Run Code Online (Sandbox Code Playgroud)

对所有矩形的所有角执行此操作,然后循环遍历所有角并计算距离(仅 abs(v1 - v2))。

我希望这可以帮助你