java - 如何使用 Java 检查两个矩形几何图形之间的空间关系

标签 java geometry

所以我有这个程序需要测试两个矩形并检查:

  1. 如果测试矩形位于引用矩形内
  2. 如果测试矩形与引用矩形重叠
  3. 如果测试矩形仅与引用矩形共享边框
  4. 如果测试矩形和引用矩形不同

引用矩形和测试矩形均由其中心坐标 (x,y) 及其宽度和高度定义。

我相信我的第一个检查编码正确,但我无法计算出最后三个检查重叠、共享边界和完全不同的数学。

这是迄今为止我的四项检查的代码:

    //returns true if the specified rectangle is inside this rectangle
    public boolean contains(MyRectangle2D r){
           if(this.x > r.x + r.width && x + width < r.x && y > r.y +r.height                    && y + height < r.y){
        return true;
    }
    else{
        return false;
    }
    }

    //returns true if the specified rectangle overlaps with this rectangle 
    public boolean overlaps(MyRectangle2D r) {
    if (this.x < r.x + r.width && x + width > r.x && y < r.y + r.height && y + height > r.y){
        return true;
    }
    else{
        return false;
    }
    }

    //returns true if only the boundaries touch
    public boolean abut(MyRectangle2D r) {
       if(this.x = r.x + r.width && x + width = r.x || y = r.y +r.height && y + height = r.y){
        return true;
    }
    else{
        return false;
    }
}

     //returns true if the rectangles are not touching at all 
     public boolean distinct(MyRectangle2D r) {

     }

最佳答案

您可以使用 Java Topolygy Suite ( JTS ) 来实现:

  • 首先,您可以使用 org.locationtech.jts.util.GeometricShapeFactory ( API ) 根据您的参数创建矩形:
  • 然后您可以使用 org.locationtech.jts.geom.Geometry ( API ) 定义的空间关系(withinoverlaps ...)

简单代码:

// method to create your rectangles like before (Polygon objects)
public static Polygon createPolygon(Coordinate center, double width, double height){
    GeometricShapeFactory shapeFactory = new GeometricShapeFactory();
    shapeFactory.setNumPoints(4);
    shapeFactory.setCentre(center);
    shapeFactory.setWidth(width);
    shapeFactory.setHeight(height);
    return shapeFactory.createRectangle();
}

public static void main(String[] args) {

    // create your rectagles
    Polygon rectangleA = createPolygon(new Coordinate(0, 0), 5, 10);
    Polygon rectangleB = createPolygon(new Coordinate(2, 0), 5, 10);

    // ### check your constraints
    // 1. rectangle is within the reference rectangle
    boolean bWithinA = rectangleB.within(rectangleA); // false

    // 2. rectangle is overlapping the reference rectangle
    boolean bOverlappingA = rectangleB.overlaps(rectangleA); // true

    // 3. rectangle is only sharing a border with the reference rectangle
    boolean bSharesBorderA = rectangleB.touches(rectangleA); // false

    // 4. rectangle and reference rectangle are distinct
    boolean bDistinctsA = rectangleB.disjoint(rectangleA); // false 
}

关于java - 如何使用 Java 检查两个矩形几何图形之间的空间关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38800229/

相关文章:

java - 为订阅找回丢失的购买 token

java - 表单绑定(bind) : relationship with intermediate entity (nested entity) - indirect Entity creation and binding

Swift 函数不更新绘图

javascript - 如何在固定宽度和高度的div内对齐四个圆圈

python - 生成此类列表的 pythonic 方式是什么? (n 维立方体的面)

algorithm - 如何从协方差矩阵和平均位置获得最佳拟合边界框?

java - Ellipse2D 绘制精度较差

java - 沿方向行驶时减速

java - 动态 <ui :include> doesn't work

java - Java 中的 if (boolean)unreachable 语句