summaryrefslogtreecommitdiffstats
path: root/helper/Polygons.java
diff options
context:
space:
mode:
Diffstat (limited to 'helper/Polygons.java')
-rw-r--r--helper/Polygons.java34
1 files changed, 34 insertions, 0 deletions
diff --git a/helper/Polygons.java b/helper/Polygons.java
new file mode 100644
index 0000000..9d3c6ad
--- /dev/null
+++ b/helper/Polygons.java
@@ -0,0 +1,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;
+ }
+}