这是一个简单的解决方案,让你2个矩形之间的最小距离的一个示例:http://imgur.com/IcHTGII
下面是用于开发它的基本构造在你的问题。我不计算这里的最大距离,但扩展我的例子应该相当简单。这可能是一种更有效的方法,但至少我会给你一些启动的东西。
下面是Rectangle类
public class Rectangle {
final public Point upperLeftCorner;
final public Point upperRightCorner;
final public Point lowerLeftCorner;
final public Point lowerRightCorner;
final public double height;
final public double length;
public Rectangle(double upper_left_x, double upper_left_y, double height, double length)
{
upperLeftCorner = new Point(upper_left_x, upper_left_y);
upperRightCorner = new Point(upper_left_x + length, upper_left_y);
lowerLeftCorner = new Point(upper_left_x, upper_left_y - height);
lowerRightCorner = new Point(upper_left_x + length, upper_left_y - height);
this.height = height;
this.length = length;
}
//returns true if this and r overlap on the horizontal axis
public boolean overlapsHorizontally(Rectangle r)
{
return Math.abs(upperLeftCorner.x - r.upperLeftCorner.x) <= Math.max(length, r.length);
}
//returns true if this and r overlap on the vertical axis
public boolean overlapsVertically(Rectangle r)
{
return Math.abs(upperLeftCorner.y - r.lowerLeftCorner.y) <= Math.max(height, r.height);
}
public double minDistanceFrom(Rectangle r)
{
//If same rectangle
if(this == r)
{
return 0;
}
//if they overlap in both directions then they actually overlap, so distance is 0
if(overlapsHorizontally(r) && overlapsVertically(r))
{
return 0;
}
if(overlapsHorizontally(r))
{
//we know they don't overlap vertically so the result is simply the vertical distance between the 2 closest horizontal sides
if(upperLeftCorner.y > r.upperLeftCorner.y)
{
return lowerLeftCorner.y - r.upperLeftCorner.y;
}
return r.lowerLeftCorner.y - upperLeftCorner.y;
}
else if(overlapsVertically(r))
{
//we know they don't overlap horizontally so the result is simply the horizontal distance between the 2 closest vertical sides
if(upperLeftCorner.x < r.upperLeftCorner.x)
{
return r.upperLeftCorner.x - upperRightCorner.x;
}
return upperLeftCorner.x - r.upperRightCorner.x;
}
else //shitty case where they don't overlap in any direction
{
//this is above r
if(upperLeftCorner.y > r.upperLeftCorner.y)
{
//this is above and to the left of r
if(upperLeftCorner.x < r.upperLeftCorner.x)
{
return lowerRightCorner.getDistanceFrom(r.upperLeftCorner);
}
else //this is above and to the right of r
{
return lowerLeftCorner.getDistanceFrom(r.upperRightCorner);
}
}
else //this is below r
{
//this is below and to the left of r
if(upperLeftCorner.x < r.upperLeftCorner.x)
{
return upperRightCorner.getDistanceFrom(r.lowerLeftCorner);
}
else //this is below and to the right of r
{
return upperLeftCorner.getDistanceFrom(r.lowerRightCorner);
}
}
}
}
}
和Point类
public class Point
{
final public double x;
final public double y;
public Point(double _x, double _y)
{
x = _x;
y = _y;
}
public double getDistanceFrom(Point p)
{
double distance = Math.sqrt(Math.pow(x - p.x, 2) + Math.pow(y - p.y, 2));
return distance;
}
}
祝你好运与您的项目。
[为什么“有人可以帮我吗?”不是一个真正的问题?](http://meta.stackoverflow.com/q/284236) – Pshemo
什么是*两个矩形之间最大可能的距离?除非你以某种方式限制它,否则总会有比你走的路更长的路。 –
@AndyTurner最大的距离应该像一个无限的,但实际上它应该是每个“角落”到另一个矩形的角落相应的距离。例如:http://imgur.com/a/cjkAr 对不起,如果我没有理解你的问题的含义或误解为其他东西 –