PathIterator を使用して任意の Shape オブジェクトの中心を計算し、曲線のパスを考慮できるようにしようとしていますが、標準の 1x1 長方形の中心を見つけると、getCenter() メソッドがポイントを返します。
Point2D.Double[0.3333333333333333, 0.3333333333333333]
私の getCenter() メソッド:
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());
}
points.toString() を印刷すると、コンソールに次のように表示されることがわかりました。
[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]]
入力 Shape オブジェクトが Rectangle2D.Double(0, 0, 1, 1) であることを考えると、points 配列には 4 つではなく、6 つのエントリがあることに気付きました。明らかに、それはポイント (0, 0) を私が望むよりも 2 回多く説明していて、それがなぜなのか混乱しています。PathIterator.isDone() メソッドの結果ですか? 私はそれを間違って使用していますか?PathIterator ができない場合、どうすれば問題を解決できますか?