I am trying to use the PathIterator to calculate the center of any Shape object, so that curved paths can be accounted for, but upon finding the center of a standard 1x1 rectangle, my getCenter() method returns the point:
Point2D.Double[0.3333333333333333, 0.3333333333333333]
My getCenter() method:
shape = new Rectangle2D.Double(0, 0, 1, 1);
public Point2D.Double getCenter()
{
ArrayList<Point2D.Double> points = new ArrayList<Point2D.Double>();
double[] arr = new double[6];
for(PathIterator pi = shape.getPathIterator(null); !pi.isDone(); pi.next())
{
pi.currentSegment(arr);
points.add(new Point2D.Double(arr[0], arr[1]));
}
double cX = 0;
double cY = 0;
for(Point2D.Double p : points)
{
cX += p.x;
cY += p.y;
}
System.out.println(points.toString());
return new Point2D.Double(cX / points.size(), cY / points.size());
}
I have discovered that upon printing points.toString(), I get this in the Console:
[Point2D.Double[0.0, 0.0], Point2D.Double[1.0, 0.0], Point2D.Double[1.0, 1.0], Point2D.Double[0.0, 1.0], Point2D.Double[0.0, 0.0], Point2D.Double[0.0, 0.0]]
I noticed that there are six entries in the points array, as opposed to four which I was expecting, given that the input Shape object is Rectangle2D.Double(0, 0, 1, 1). Obviously it is accounting for the point (0, 0) two more times than I want it to, and I am confused as to why that is. Is it a result of the PathIterator.isDone() method? Am I using it incorrectly? What would solve my problem if PathIterator can't?