compute the mean (average) of a point array - Java java.lang

Java examples for java.lang:Math Array Function

Description

compute the mean (average) of a point array

Demo Code


import javax.vecmath.Point2d;
import javax.vecmath.Tuple2d;
import javax.vecmath.Vector2d;

public class Main{
    /**/*from w w  w  .  ja v  a  2  s.  c  o m*/
     * compute the mean (average) of a point array
     * @param points points
     * @return a newly created point = sum (points) / point count
     */
    public static Point2d mean(final Point2d... points) {
        Point2d sum = new Point2d();
        for (Point2d p : points) {
            add(sum, sum, p);
        }
        return div(sum, sum, points.length);
    }
    /**
     * add two points
     * @param p point to fill
     * @param pA first point to add
     * @param pB second point to add
     */
    public static Point2d add(final Point2d p, final Point2d pA,
            final Tuple2d pB) {
        p.x = pB.x + pA.x;
        p.y = pB.y + pA.y;
        return p;
    }
    /**
     * add two points
     * @param p point to fill
     * @param x1Pixel first point to add
     * @param nPixel second point to add
     */
    public static Point2d add(final Point2d pA, final Tuple2d v) {
        return add(new Point2d(), pA, v);
    }
    /**
     * div two points
     * @param p point to fill
     * @param pA first point to div
     * @param x divider
     */
    public static Point2d div(final Point2d p, final Point2d pA,
            final double x) {
        p.x = pA.x / x;
        p.y = pA.y / x;
        return p;
    }
    /**
     * div two points
     * @param p point to fill
     * @param pA first point to div
     * @param x divider
     */
    public static Point2d div(final Point2d pA, final double x) {
        return div(new Point2d(), pA, x);
    }
}

Related Tutorials