average Radius - Java 2D Graphics

Java examples for 2D Graphics:Arc

Description

average Radius

Demo Code


//package com.java2s;

import java.awt.geom.*;

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

    public static double averageRadius(GeneralPath generalPath) {

        Point2D.Double center = center(generalPath
                .getPathIterator(identity));
        PathIterator i = generalPath.getPathIterator(identity);

        double[] coords = new double[6];
        double rTotal = 0;
        int count = 0;
        while (!i.isDone()) {
            int segType = i.currentSegment(coords);
            if (segType != PathIterator.SEG_CLOSE) {
                double x = center.x - coords[0];
                double y = center.y - coords[1];
                rTotal += Math.sqrt(x * x + y * y);
                count++;/*from   ww  w.j  a  v a 2 s .c o  m*/
            }
            i.next();
        }

        return rTotal / count;

    }

    public static Point2D.Double center(GeneralPath generalPath) {
        return center(generalPath.getPathIterator(identity));
    }

    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