Lua 如何计算两个矩形之间的距离?(背景:卢阿的一场游戏。)
给定两个矩形,x、y、宽度、高度(以像素为单位)和旋转值(以度为单位)——如何计算它们的轮廓彼此之间的最近距离 背景:在一个用Lua编写的游戏中,我随机生成地图,但我想确保某些矩形彼此不太接近——这是必要的,因为如果矩形进入某个近距离位置,地图将无法求解,因为球需要在它们之间通过。速度不是一个大问题,因为我没有很多矩形,地图只在每个级别生成一次。我以前在StackOverflow上找到的链接是和Lua 如何计算两个矩形之间的距离?(背景:卢阿的一场游戏。),lua,distance,rectangles,Lua,Distance,Rectangles,给定两个矩形,x、y、宽度、高度(以像素为单位)和旋转值(以度为单位)——如何计算它们的轮廓彼此之间的最近距离 背景:在一个用Lua编写的游戏中,我随机生成地图,但我想确保某些矩形彼此不太接近——这是必要的,因为如果矩形进入某个近距离位置,地图将无法求解,因为球需要在它们之间通过。速度不是一个大问题,因为我没有很多矩形,地图只在每个级别生成一次。我以前在StackOverflow上找到的链接是和 非常感谢 这个问题取决于什么样的距离。您想要,中心距离,边缘距离还是最近角的距离 我想你是指最后一个
非常感谢 这个问题取决于什么样的距离。您想要,中心距离,边缘距离还是最近角的距离 我想你是指最后一个。如果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.
对所有矩形的所有角点执行此操作,然后在所有角点上循环并计算距离(仅abs(v1-v2))
我希望这对您有所帮助伪代码:
矩形之间的距离=一些可怕的大数字;
对于矩形1中的每个边1:
对于矩形2中的每个边2:
距离=计算最短距离
if(距离<矩形之间的距离)
矩形之间的距离=距离编辑:如OK所指出的,此解决方案假设所有矩形都是竖直的。要使其适用于OP要求的旋转矩形,还必须计算从每个矩形的角到另一个矩形最近边的距离。但是,在大多数情况下,如果该点位于线段的两个端点上方或下方,并且位于两个线段的左侧或右侧(相对于线段的电话位置1、3、7或9),则可以避免进行该计算 Agnius的答案依赖于lineSegments()函数之间的距离。以下是一个案例分析,它不:
(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).
有很多算法可以解决这个问题,Agnius算法工作得很好。然而,我更喜欢下面的内容,因为它看起来更直观(你可以在一张纸上完成),它们不依赖于寻找线之间的最小距离,而是点和线之间的距离 困难的部分是实现数学函数,以确定直线和点之间的距离,以及确定点是否面向直线。不过,你可以用简单的三角法来解决这一切。我有以下方法来做这件事 用于任意角度的多边形(三角形、矩形、六边形等)
Area=12计算高度⋅基础⋅高度
,底部为边缘长度如果您有兴趣,我可以用Python实现所有这些功能。不使用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.
def rect_距离((x1,y1,x1b,y1b),(x2,y2,x2b,y2b)):
左=x2b
在哪里
是点之间的欧几里德距离dist
- 矩形。1由点
和(x1,y1)
(x1b,y1b)
- 矩形。2由点
和(x2,y2)
(x2b,y2b)
- 事实上,有一个快速的数学解
Length(Max((0, 0), Abs(Center - otherCenter) - (Extent + otherExtent)))
其中Center=((最大-最小)/2)+Minimum
和Extent=(最大-最小)/2
。
基本上零轴以上的代码是重叠的,因此距离总是正确的
最好将矩形保持为这种格式,因为在许多情况下(例如旋转更容易)更可取。请检查Java,它有一个约束:所有矩形都是平行的,所有相交的矩形都返回0:
public static double findClosest(Rectangle rec1, Rectangle rec2) {
double x1, x2, y1, y2;
double w, h;
if (rec1.x > rec2.x) {
x1 = rec2.x; w = rec2.width; x2 = rec1.x;
} else {
x1 = rec1.x; w = rec1.width; x2 = rec2.x;
}
if (rec1.y > rec2.y) {
y1 = rec2.y; h = rec2.height; y2 = rec1.y;
} else {
y1 = rec1.y; h = rec1.height; y2 = rec2.y;
}
double a = Math.max(0, x2 - x1 - w);
double b = Math.max(0, y2 - y1 - h);
return Math.sqrt(a*a+b*b);
}
我只是用n维写了代码。我不容易找到一个普遍的解决办法
//考虑包含两个点(最小值和最大值)的矩形对象
双距离(常数矩形和a、常数矩形和b)常数{
//w