Java中矩形邻接的检测

Java中矩形邻接的检测,java,algorithm,geometry,shapes,Java,Algorithm,Geometry,Shapes,我使用正确的相交(矩形)和包含(矩形)方法实现了以下矩形POJO: public class Rectangle { private double x; // x-value of upper-left corner of rectangle private double y; // y-value of upper-left corner of rectangle private double width; // width of the r

我使用正确的
相交(矩形)
包含(矩形)
方法实现了以下
矩形
POJO:

public class Rectangle {
    private double x;       // x-value of upper-left corner of rectangle
    private double y;       // y-value of upper-left corner of rectangle
    private double width;   // width of the rectangle
    private double height;  // height of the rectangle

    // Returns true if this Rectangle intersects otherRectangle.
    public boolean intersects(Rectangle otherRectangle) {
        double x = otherRectangle.getX();
        double y = otherRectangle.getY();
        double w = otherRectangle.getWidth();
        double h = otherRectangle.getHeight();
        double x0 = getX();
        double y0 = getY();

        if(isEmpty() || w <= 0 || h <= 0)
            return false;

        return (
            x + w > x0 &&
            y + h > y0 &&
            x < x0 + getWidth() &&
            y < y0 + getHeight()
        );
    }

    // Returns true if this Rectangle contains otherRectangle.
    public boolean contains(Rectangle otherRectangle) {
        double x = otherRectangle.getX();
        double y = otherRectangle.getY();
        double w = otherRectangle.getWidth();
        double h = otherRectangle.getHeight();
        double x0 = getX();
        double y0 = getY();

        return (
            x >= x0 &&
            y >= y0 &&
            x < x0 + getWidth() &&
            y < y0 + getHeight()
        );
    }

    // Returns true if this Rectangle is adjacent to otherRectangle.
    public boolean isAdjacentTo(Rectangle otherRectangle) {
        // ???
    }
}
公共类矩形{
private double x;//矩形左上角的x值
私有双y;//矩形左上角的y值
私有双宽度;//矩形的宽度
private double height;//矩形的高度
//如果此矩形与其他矩形相交,则返回true。
公共布尔相交(矩形或矩形){
double x=otherRectangle.getX();
双y=otherRectangle.getY();
double w=otherRectangle.getWidth();
双h=otherRectangle.getHeight();
双x0=getX();
双y0=getY();
如果(isEmpty()| | w y0&&
x=x0&&
y>=y0&&
x
现在我正试图实现
isappachentto
,我感到窒息。另一个SOer告诉我,对于邻接,我可以:

…只需在一个轴上进行包容检查(如顶部和底部),然后确保每个角不包含在另一个方向(如水平方向)


但我仍然没有看到解决方案。有什么想法吗?最理想的情况是,我可以使用
相交
包含
方法,但我会选择任何真正有效的方法

好的。我将编写一些方法
leftSide()
rightSide()
topSide()
bottomSide()
,它们分别是矩形的左侧、右侧、顶部和底部(底部在数字上比顶部小,但如果它显示在屏幕上,它将是顶部)

然后代码:

// Returns true if this Rectangle is adjacent to otherRectangle.
public boolean isAdjacentTo(Rectangle otherRectangle, double tolerance) {
    if(Math.abs(getLeftSide()-otherRectangle.getRightSide())<tolerance||Math.abs(otherRectangle.getLeftSide()-getRightSide())<tolerance)
    {
        return !(getTopSide()<otherRectangle.getBottomSide()||otherRectangle.getTopSide()<getBottomSide());
    }
    if(Math.abs(getTopSide()-otherRectangle.getBottomSide())<tolerance||Math.abs(otherRectangle.getTopSide()-getBottomSide())<tolerance)
    {
        return !(getRightSide()<otherRectangle.getLeftSide()||otherRectangle.getRightSide()<getLeftSide());
    }
    return false;
}
//如果此矩形与其他矩形相邻,则返回true。
公共布尔值IsAjacentto(矩形或矩形,双公差){

if(Math.abs(getLeftSide()-otherRectangle.getRightSide())你所说的相邻到底是什么意思?你的意思是它们共用一条边吗?角是否一定在同一个位置?如果边重叠,但一条边不一定包含在另一条边上怎么办?Hi@jgon(+1表示提出了一些好的观点).我想我不完全理解你的问题,所以让我问你一些作为回报。一旦我理解你的问题,我将更新我的问题以反映缺少的任何信息…1.是的,
isappachentto
,我的意思是“共享一方”.2.角:不知道你这是什么意思?你能给我一个例子,说明你说“角必须在同一个地方吗?”.3.“如果边重叠怎么办…”?我认为这是我困惑的根源。对我来说,当两个矩形共享同一侧时,它们共享的一侧是同一条直线。因为点是无限小的,而一条直线是点的集合,那么一条直线就不能有任何宽度。所以……如果它们共享同一侧,它们不必重叠吗?!两个形状如何共享一侧,但不能重叠帽子边是两条不同的线?否则,两边之间总是会有一个很小的间隙,它们不会共享/相邻!