This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
centre in any shape - Java2D + Center of Attention
Posted by vijay on December 30, 2001 at 2:33 AM
Thanks for all the help. Here is the solution. IF there is an error please do correct
public static Point2D[] getCentroidOfShape(Shape[] theShapes) { Point2D[] centroids = new Point2D.Float[theShapes.length]; for (int i = 0; i < theShapes.length; i++) { PathIterator pi = theShapes[i].getPathIterator(null); FlatteningPathIterator fpt = new FlatteningPathIterator(pi,.1,20); int j = 0; // number of points double dSumX = 0; double dSumY = 0; while(!fpt.isDone()) { double[] coordinates = new double[2]; int type = fpt.currentSegment(coordinates); if ( type == PathIterator.SEG_LINETO || type == PathIterator.SEG_MOVETO ) { j++; dSumX = dSumX + coordinates[0]; dSumY = dSumY + coordinates[1]; } fpt.next(); } centroids[i] = new Point2D.Float((float) (dSumX/j),(float)(dSumY/j)); } return centroids; }
Thanks to all
Replies:
|