如何计算两个矩形之间的距离? (上下文:Lua 中的游戏。)
给定两个具有 x、y、宽度、高度(以像素为单位)和旋转值(以度为单位)的矩形 — 如何计算它们轮廓彼此之间的最近距离?
背景:在用 Lua 编写的游戏中,我随机生成地图,但希望确保某些矩形彼此之间距离不太近——这是必需的,因为如果矩形进入某个近距离位置,地图将变得无法解析,如下所示需要有一个球在它们之间通过。速度不是一个大问题,因为我没有很多矩形,而且地图每个级别只生成一次。我之前在 StackOverflow 上找到的链接是 this 和 < a href="https://stackoverflow.com/questions/84034/what-is-the-quickest-way-to-find-the-shortest-cartesian-distance- Between-two-poly">这个
非常感谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
Lua 中没有,基于 M Katz 建议的 Python 代码:
其中
dist
是点 rect 之间的欧几里得距离(x1, y1)
和(x1b, y1b)
矩形组成(x2, y2)
和(x2b, y2b)
组成Not in Lua, a Python code based on M Katz's suggestion:
where
dist
is the euclidean distance between points(x1, y1)
and(x1b, y1b)
(x2, y2)
and(x2b, y2b)
编辑:正如 OK 所指出的,此解决方案假设所有矩形都是直立的。为了使其适用于OP要求的旋转矩形,您还必须计算从每个矩形的角到另一个矩形的最近边的距离。但在大多数情况下,如果该点在线段的两个端点上方或下方,并且位于两条线段的左侧或右侧(相对于电话位置 1、3、7 或 9),则可以避免进行该计算。线段)。
Agnius 的答案依赖于 DistanceBetweenLineSegments() 函数。下面是一个案例分析,没有:
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:
实际上有一个快速的数学解决方案。
其中
中心 = ((最大值 - 最小值)/2) + 最小值
和范围 = (最大值 - 最小值)/2
。基本上,零轴上方的代码是重叠的,因此距离始终是正确的。
最好将矩形保留为这种格式,因为它在许多情况下更可取(ae 旋转更容易)。
Actually there is a fast mathematical solution.
Where
Center = ((Maximum - Minimum) / 2) + Minimum
andExtent = (Maximum - Minimum) / 2
.Basically the code above zero's axis which are overlapping and therefore the distance is always correct.
It's preferable to keep the rectangle in this format as it's preferable in many situations ( a.e. rotations are much easier ).
伪代码:
distance_ Between_rectangles = some_scary_big_number;
对于 Rectangle1 中的每条边 1:
对于 Rectangle2 中的每条边 2:
距离=计算最短边1和边2之间的距离
if(距离
矩形之间的距离 = 距离
Pseudo-code:
distance_between_rectangles = some_scary_big_number;
For each edge1 in Rectangle1:
For each edge2 in Rectangle2:
distance = calculate shortest distance between edge1 and edge2
if (distance < distance_between_rectangles)
distance_between_rectangles = distance
我解决问题的方法:
矩形
矩形,该矩形的对角线是之间的距离
两个矩形。
这是一个 C# 示例
My approach to solving the problem:
rectangle
rectangles, the diagonal of this rectangle is the distance between
the two rectangles.
Here is an example in C#
有很多算法可以解决这个问题,Agnius 算法效果很好。不过,我更喜欢下面的,因为它看起来更直观(您可以在一张纸上完成),并且它们不依赖于找到线之间的最小距离,而是依赖于点和线之间的距离。
困难的部分是实现数学函数来查找直线和点之间的距离,以及查找点是否面向直线。不过,您可以用简单的三角函数来解决所有这些问题。我有以下方法来做到这一点。
对于任意角度的多边形(三角形、矩形、六边形等)
只要形状的任何两条边所形成的角度不超过 180 度,这些算法就会起作用。原因是,如果某物的角度超过 180 度,则意味着某些角内部膨胀,就像恒星一样。
边与点之间的最小距离
Area = 12⋅base⋅height
计算高度,其中base
是边的长度。检查一个点是否面向一条边
与之前一样,您可以从一条边和一个点创建一个三角形。现在使用余弦定律,只需知道边缘距离即可找到所有角度。只要从边缘到该点的每个角度都低于 90 度,该点就面向边缘。
如果您有兴趣,我在此处提供了所有这些内容的 Python 实现。
There are many algorithms to solve this and Agnius algorithm works fine. However I prefer the below since it seems more intuitive (you can do it on a piece of paper) and they don't rely on finding the smallest distance between lines but rather the distance between a point and a line.
The hard part is implementing the mathematical functions to find the distance between a line and a point, and to find if a point is facing a line. You can solve all this with simple trigonometry though. I have below the methodologies to do this.
For polygons (triangles, rectangles, hexagons, etc.) in arbitrary angles
These algorithms work as long as any two edges of the shape don't create angles more than 180 degrees. The reason is that if something is above 180 degrees then it means that the some corners are inflated inside, like in a star.
Smallest distance between an edge and a point
Area = 12⋅base⋅height
withbase
being the edge's length.Check to see if a point faces an edge
As before you make a triangle from an edge and a point. Now using the Cosine law you can find all the angles with just knowing the edge distances. As long as each angle from the edge to the point is below 90 degrees, the point is facing the edge.
I have an implementation in Python for all this here if you are interested.
这个问题取决于什么样的距离。您想要中心距离、边缘距离还是最近角点距离?
我猜你指的是最后一个。如果 X 和 Y 值指示矩形的中心,那么您可以通过应用此技巧找到每个角
对所有矩形的所有角执行此操作,然后循环遍历所有角并计算距离(只需 abs(v1 - v2) )。
我希望这对你有帮助
This question depends on what kind of distance. Do you want, distance of centers, distance of edges or distance of closest corners?
I assume you mean the last one. If the X and Y values indicate the center of the rectangle then you can find each the corners by applying this trick
Do this for all corners of all rectangles, then just loop over all corners and calculate the distance (just abs(v1 - v2)).
I hope this helps you
我刚刚编写了 n 维的代码。我无法轻松找到通用解决方案。
要计算矩形和点之间的距离,您可以:
如果要旋转其中一个矩形,则需要旋转坐标系。
如果要旋转两个矩形,可以旋转矩形
a
的坐标系。然后我们必须更改这一行:因为这认为
b
中只有一个候选顶点作为最近的顶点。您必须更改它才能检查到b
中所有顶点的距离。它始终是顶点之一。请参阅:https://i.sstatic.net/EKJmr.png
I just wrote the code for that in n-dimensions. I couldn't find a general solution easily.
For calculating the distance between a rectangle and a point you can:
If you want to rotate one of the rectangles, you need to rotate the coordinate system.
If you want to rotate both rectangles, you can rotate the coordinate system for rectangle
a
. Then we have to change this line:because this considers there is only one candidate as the closest vertex in
b
. You have to change it to check the distance to all vertexes inb
. It's always one of the vertexes.See: https://i.sstatic.net/EKJmr.png
请检查 Java,它有所有矩形都是平行的约束,对于所有相交的矩形它返回 0:
Please check this for Java, it has the constraint all rectangles are parallel, it returns 0 for all intersecting rectangles:
另一种解决方案是计算矩形上的多个点并选择距离最小的点对。
优点:适用于所有多边形。
缺点:准确度稍差,速度较慢。
Another solution, which calculates a number of points on the rectangle and choses the pair with the smallest distance.
Pros: works for all polygons.
Cons: a little bit less accurate and slower.