I work with a lot of 2D floating point polygons. I came up with a use case where I need to subtract one from another, so I thought I'd use java.awt.geom.Area. I create an Area object with four points:
100.0, 50.0
150.0, 0.0
151.41421356237308, 2.8284271247461112
99.99999999999973, 54.242640687118936
And independent of how I order those points when creating the Area, I get back the following:
SEG_MOVETO, 150.0, 0.0
SEG_LINETO, 100.0, 50.0
SEG_LINETO, 99.99999999999973, 54.24264068711893
SEG_LINETO, 99.99999999999974, 54.24264068711893
SEG_LINETO, 151.41421356237308, 2.8284271247461112
SEG_LINETO, 150.0, 0.0
SEG_CLOSE, 150.0, 0.0
Note the almost identical dual 99.99999999999973, 54.24264068711893
coordinates.
Any clues in how to avoid that would be most welcome. Here's the code:
import java.awt.geom.Area;
import java.awt.geom.Path2D;
import java.awt.geom.PathIterator;
class Main {
public static final void main( String args[] ) {
double[] myPoly = {100.0, 50.0, 150.0, 0.0, 151.41421356237308, 2.8284271247461112, 99.99999999999973, 54.242640687118936};
final Area myArea = makeArea(myPoly);
System.out.println(areaToString(myArea));
}
private static Area makeArea(double coords[]) {
final Path2D path = new Path2D.Double();
path.moveTo(coords[0], coords[1]);
for (int i = 2; i < coords.length; i+=2) {
path.lineTo(coords[i], coords[i+1]);
}
path.closePath();
return new Area(path);
}
private static String areaToString(final Area area) {
final StringBuffer out = new StringBuffer("Area [\n");
double []pt = new double[6];
for (PathIterator pi = area.getPathIterator(null); !pi.isDone(); pi.next()) {
int type = pi.currentSegment(pt);
out.append(type).append(", ").append(pt[0]).append(", ").append(pt[1]).append("\n");
}
return out.append(']').toString();
}
}