Calculate area of a convex polygon - Java java.lang

Java examples for java.lang:Math Geometry Shape

Description

Calculate area of a convex polygon

Demo Code

/***//from  w w  w.j  av  a  2 s.c om
 * Copyright (C) 2010 Johan Henriksson
 * This code is under the Endrov / BSD license. See www.endrov.net
 * for the full text and how to cite.
 */
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.vecmath.Vector3d;

public class Main{
    /**
     * Calculate area of a convex polygon
     */
    public static double polygonArea(Vector3d[] vv) {
        double area = 0;
        Vector3d vA = vv[0];
        for (int i = 1; i < vv.length - 1; i++) {
            Vector3d vAB = new Vector3d(vv[i]);
            Vector3d vAC = new Vector3d(vv[i + 1]);
            vAB.sub(vA);
            vAC.sub(vA);
            double x = vAB.dot(vAC);
            area += Math.sqrt(vAB.lengthSquared() * vAC.lengthSquared() - x
                    * x);
        }
        return area * 0.5;
    }
}

Related Tutorials