简体   繁体   English

将Rectangle2D与java.awt.geom.Area一起使用时,区域的结果不正确

[英]Incorrect result of area when using java.awt.geom.Area with Rectangle2D

I was recently using the Java Area class to wrap up a Rectangle2D.Double type. 我最近正在使用Java Area类包装Rectangle2D.Double类型。 So that I can do manipulations like intersect, add, etc. However, when it comes to the calculation of the area of the shape, I was getting a quite weird result. 这样我就可以进行相交,相加等操作。但是,在计算形状面积时,我得到了一个非常奇怪的结果。 Below is the code I'm using to calculate the area of a shape: 以下是我用来计算形状面积的代码:

private static double polyArea(ArrayList<Point2D.Double> pointList) {
    double area = 0;
    for (int loopi = 1; loopi < pointList.size(); loopi++) {
        Point2D.Double p1 = pointList.get(loopi - 1);
        Point2D.Double p2 = pointList.get(loopi);
        area += (p1.x * p2.y - p2.x * p1.y) / 2.0;
    }
    return area;
}

public  static double coverageArea(Shape s) {
    ArrayList<Point2D.Double> pointList = new ArrayList<Point2D.Double>();
    double[] coords = new double[6];
    int type;
    double totalArea = 0;
    PathIterator it = s.getPathIterator(null);
    while (!it.isDone()) {
        type = it.currentSegment(coords);
        if (type == it.SEG_MOVETO) {
            pointList.clear();
            pointList.add(new Point2D.Double(coords[0], coords[1]));
        } else if (type == it.SEG_LINETO) {
            pointList.add(new Point2D.Double(coords[0], coords[1]));
        } else if (type == it.SEG_CLOSE) {
            totalArea += polyArea(pointList);
            pointList.clear();
        } else {
            System.out.println("calculateShapeArea: Cannot calculate area for shapes with segment type other than SEG_MOVETO, SEG_LINETO, or SEG_CLOSE.  Ignoring segment type=" + type);
        }
        it.next();
    }
    if (totalArea < 0) {
        totalArea = -totalArea;
    }
    return totalArea;
}

If I have a Rectangle2D r(1.0, 1.0, 6.0, 6.0), using the above code I will get the area correctly 36. However if I do a = new Area(r) , then the result of coverageArea(a) is 39. Sometimes it could be tens of times larger than the correct answer. 如果我有一个Rectangle2D r(1.0,1.0,6.0,6.0),使用上面的代码,我将正确获得36的面积。但是,如果我做a = new Area(r) ,则coverageArea(a)的结果为39有时它可能比正确答案大几十倍。

Anyone knows why this is happening? 有人知道为什么会这样吗? Is there any problem with the area calculation? 面积计算有问题吗? Any advice would be appreciated! 任何意见,将不胜感激!

According to this Wiki , your code is not implementing the method correctly. 根据此Wiki ,您的代码未正确实现该方法。 Your polyArea() method forget to close the polygon (it does not consider the line from the last to the first vertex). 您的polyArea()方法忘记关闭多边形(它不考虑从最后 一个顶点到第一个顶点的线)。

Also your version of the formula seems to have exchanged p1 and p2, although I'm not sure if thats a problem, I don't personally understand how this method really works. 此外,您的公式的版本似乎有交换p1和p2,虽然我不知道如果多数民众赞成的问题,我不亲自了解这种方法确实有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM