center of GeneralPath - Java 2D Graphics

Java examples for 2D Graphics:Path

Description

center of GeneralPath

Demo Code


//package com.java2s;

import java.awt.geom.*;

public class Main {
    private static final AffineTransform identity = new AffineTransform();

    public static Point2D.Double center(GeneralPath generalPath) {
        return center(generalPath.getPathIterator(identity));
    }/* www . j a  v a2  s.c  om*/

    public static Point2D.Double center(PathIterator i) {
        double[] coords = new double[6];
        double xTotal = 0;
        double yTotal = 0;
        int count = 0;
        while (!i.isDone()) {
            int segType = i.currentSegment(coords);
            if (segType != PathIterator.SEG_CLOSE) {
                xTotal += coords[0];
                yTotal += coords[1];
                count++;
            }
            i.next();
        }

        return new Point2D.Double(xTotal / count, yTotal / count);

    }
}

Related Tutorials