get Area of Polygon - Java 2D Graphics

Java examples for 2D Graphics:Polygon

Description

get Area of Polygon

Demo Code


//package com.java2s;

import java.awt.Polygon;

import java.util.List;

public class Main {
    public static double getArea(List<Polygon> polygons) {
        double ta = 0D;
        for (Polygon p : polygons) {
            double a = getArea(p);
            ta += a;/*from w ww.  j  a  v  a 2 s .  com*/
        }
        return ta;
    }

    public static double getArea(Polygon p) {
        double a = 0D;
        for (int i = 0; i < p.npoints; i++) {
            int j = i == p.npoints - 1 ? 0 : i + 1;
            double xi = (double) p.xpoints[i];
            double yj = (double) p.ypoints[j];
            double xj = (double) p.xpoints[j];
            double yi = (double) p.ypoints[i];

            a += (xi * yj - xj * yi);
        }
        a /= 2D;
        return a;
    }
}

Related Tutorials