矩形之间的最小距离

时间:2017-04-02 21:23:39

标签: java

我目前正在处理一个涉及矩形的项目,我想创建一个公共方法,它将另一个矩形作为定义,并返回它们之间可能的最小距离。 (可以通过每个轴上每个投影的较小可能距离来计算)。

我还需要另一种方法来返回2个矩形之间的最大可能距离。我对所需的复杂性感到困惑,不知道如何构建它并想要一些帮助。

这是一个例子:http://imgur.com/IcHTGII

以下是用于开发它的基本构造函数:

class Rectangle { 
    private int dimensionality; 
    private double[] lowerBound; 
    private double[] upperBound; 

    public Rectangle(int dimensionality){ 
        this.dimensionality = dimensionality; 
        lowerBound = new double[dimensionality]; 
        upperBound = new double[dimensionality]; 
    } 
}

1 个答案:

答案 0 :(得分:0)

这是一个简单解决方案的示例,它可以为您提供问题中定义的2个矩形之间的最小距离。我不会在这里计算最大距离,但扩展我的例子应该相当简单。可能有一种更有效的方法可以做到这一点,但至少我会给你一些东西。

这是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;
    }
}

祝你的项目好运。