summaryrefslogtreecommitdiffstats
path: root/helper/Polygons.java
blob: 9d3c6ad348a107564d46b480c4a48caf81a4896c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package helper;

import java.awt.Polygon;

public class Polygons {
	public static double area(Polygon poly) {
		double area = 0;
		// make list out of polygon. see
		// http://anklick-bar.de/matheprojekt/kurbel-gauss.pdf
		Point[] list = new Point[poly.npoints + 2];

		for (int i = 0; i < poly.npoints; i++) {
			list[i] = new Point(poly.xpoints[i], poly.ypoints[i]);
		}
		list[poly.npoints] = new Point(poly.xpoints[0], poly.ypoints[0]);
		list[poly.npoints + 1] = new Point(poly.xpoints[1], poly.ypoints[1]);
		for (int i = 1; i <= poly.npoints; i++) {
			// area of polygon is left to the line
			area = area + (list[i + 1].y - list[i - 1].y) * list[i].x;
		}
		return area * (-1);

	}
}

class Point {
	int x;
	int y;

	public Point(int x, int y) {
		this.x = x;
		this.y = y;
	}
}