Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Java检查两个矩形几何图形之间的空间关系_Java_Geometry - Fatal编程技术网

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

如何使用Java检查两个矩形几何图形之间的空间关系,java,geometry,Java,Geometry,所以我有一个程序需要测试两个矩形并检查: 如果测试矩形在参考矩形内 如果测试矩形与参考矩形重叠 如果测试矩形仅与参考矩形共享边框 如果测试矩形和参考矩形不同 参考矩形和测试矩形均使用其中心坐标(x,y)及其宽度和高度进行定义 我相信我的第一个检查编码正确,但我无法计算出重叠、共享边界和完全不同的最后三个检查的数学 以下是我迄今为止四项检查的代码: //returns true if the specified rectangle is inside this rectangle

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

  • 如果测试矩形在参考矩形内
  • 如果测试矩形与参考矩形重叠
  • 如果测试矩形仅与参考矩形共享边框
  • 如果测试矩形和参考矩形不同
  • 参考矩形和测试矩形均使用其中心坐标(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) {
    
         }
    
    //如果指定的矩形在此矩形内,则返回true
    公共布尔包含(MyRectangler){
    如果(此.x>r.x+r.width&&x+widthr.y+r.height&&y+heightr.x&&yr.y){
    返回true;
    }
    否则{
    返回false;
    }
    }
    //如果仅边界接触,则返回true
    公共布尔邻接(MyRectangler){
    如果(此.x=r.x+r.width&&x+width=r.x | | y=r.y+r.height&&y+height=r.y){
    返回true;
    }
    否则{
    返回false;
    }
    }
    //如果矩形完全不接触,则返回true
    公共布尔值独立(MyRectangler){
    }
    
    您可以使用Java Topology套件()来实现:

    • 首先,您可以使用
      org.locationtech.jts.util.GeometricShapeFactory
      ()创建带有参数的矩形:
    • 然后您可以使用
      org.locationtech.jts.geom.Geometry
      ()
    简单代码:

    // 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 Topology套件()来实现:

    • 首先,您可以使用
      org.locationtech.jts.util.GeometricShapeFactory
      ()创建带有参数的矩形:
    • 然后您可以使用
      org.locationtech.jts.geom.Geometry
      ()
    简单代码:

    // 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 
    }