changeset 1797:5eec623db50a

Polygon2D: moved 2D vector operation to separate class. flys-artifacts/trunk@3120 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 31 Oct 2011 10:03:32 +0000
parents ae6ace900c07
children 552888e9c64a
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/geom/Polygon2D.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/geom/VectorUtils.java
diffstat 3 files changed, 93 insertions(+), 60 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Mon Oct 31 10:01:08 2011 +0000
+++ b/flys-artifacts/ChangeLog	Mon Oct 31 10:03:32 2011 +0000
@@ -1,3 +1,11 @@
+2011-10-31	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/geom/VectorUtils.java:
+	  New. Vector operations on Point2D.
+
+	* src/main/java/de/intevation/flys/artifacts/geom/Polygon2D.java:
+	  Moved vector operations to VectorUtils.
+
 2011-10-31	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/model/WKmsFactory.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/geom/Polygon2D.java	Mon Oct 31 10:01:08 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/geom/Polygon2D.java	Mon Oct 31 10:03:32 2011 +0000
@@ -16,8 +16,6 @@
 public class Polygon2D
 implements   Serializable
 {
-    public static final double EPSILON = 1e-4;
-
     private static final double X(Point2D p) {
         return p.getX();
     }
@@ -52,11 +50,6 @@
         points = new ArrayList<Point2D>();
     }
 
-    public static boolean epsilonEquals(Point2D a, Point2D b) {
-        return Math.abs(X(a)-X(b)) < EPSILON 
-            && Math.abs(Y(a)-Y(b)) < EPSILON;
-    }
-
     public void add(double x, double y) {
         points.add(new Point2D.Double(x, y));
     }
@@ -67,7 +60,7 @@
                 points.add(p);
                 return true;
             case 1:
-                if (epsilonEquals(points.get(0), p)) {
+                if (VectorUtils.epsilonEquals(points.get(0), p)) {
                     return false;
                 }
                 points.add(p);
@@ -75,11 +68,11 @@
             default:
                 int L = points.size()-1;
                 Point2D last = points.get(L);
-                if (epsilonEquals(last, p)) {
+                if (VectorUtils.epsilonEquals(last, p)) {
                     return false;
                 }
                 Point2D before = points.get(L-1);
-                if (collinear(before, last, p)) {
+                if (VectorUtils.collinear(before, last, p)) {
                     points.set(L, p);
                 }
                 else {
@@ -95,56 +88,6 @@
         }
     }
 
-    private static final double L1(Point2D a, Point2D b) {
-        return Math.abs(X(a)-X(b)) + Math.abs(Y(a)-Y(b));
-    }
-
-    public static boolean collinear(Point2D a, Point2D b, Point2D c) {
-        double dab = L1(a, b);
-        double dac = L1(a, c);
-        double dbc = L1(b, c);
-
-        Point2D p1, p2, p3;
-
-        if (dab > dac) {
-            if (dab > dbc) {
-                p1 = a;
-                p2 = b;
-                p3 = c;
-            }
-            else { // dbc >= dab
-                p1 = b;
-                p2 = c;
-                p3 = a;
-            }
-        }
-        else { // dac >= dab
-            if (dac > dbc) {
-                p1 = a;
-                p2 = c;
-                p3 = b;
-            }
-            else { // dbc >= dac
-                p1 = b;
-                p2 = c;
-                p3 = a;
-            }
-        }
-
-        // TODO: Continue here.
-
-
-        return true;
-    }
-
-    public static Point2D sub(Point2D a, Point2D b) {
-        return new Point2D.Double(X(a)-X(b), Y(a)-Y(b));
-    }
-
-    public static Point2D scale(Point2D a, double s) {
-        return new Point2D.Double(s*X(a), s*Y(a));
-    }
-
     public double area() {
         double area = 0d;
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/geom/VectorUtils.java	Mon Oct 31 10:03:32 2011 +0000
@@ -0,0 +1,82 @@
+package de.intevation.flys.geom;
+
+import java.awt.geom.Point2D;
+
+public final class VectorUtils
+{
+    public static final double EPSILON = 1e-4;
+
+    private VectorUtils() {
+    }
+
+    private static final double X(Point2D p) {
+        return p.getX();
+    }
+
+    private static final double Y(Point2D p) {
+        return p.getY();
+    }
+
+    public static final Point2D sub(Point2D a, Point2D b) {
+        return new Point2D.Double(X(a)-X(b), Y(a)-Y(b));
+    }
+
+    public static final double dot(Point2D a, Point2D b) {
+        return X(a)*X(b) + Y(a)*Y(b);
+    }
+
+    public static final Point2D add(Point2D a, Point2D b) {
+        return new Point2D.Double(X(a)+X(b), Y(a)+Y(b));
+    }
+
+    public static final Point2D negate(Point2D a) {
+        return new Point2D.Double(-X(a), -Y(a));
+    }
+
+    public static final Point2D ortho(Point2D a) {
+        return new Point2D.Double(-Y(a), X(a));
+    }
+
+    public static final Point2D scale(Point2D a, double s) {
+        return new Point2D.Double(s*X(a), s*Y(a));
+    }
+
+    public static final double lengthSq(Point2D a) {
+        double x = X(a);
+        double y = Y(a);
+        return x*x + y*y;
+    }
+
+    public static final double length(Point2D a) {
+        return Math.sqrt(lengthSq(a));
+    }
+
+    public static final Point2D normalize(Point2D a) {
+        double length = length(a);
+        return length != 0d
+            ? scale(a, 1d/length) 
+            : new Point2D.Double(X(a), Y(a));
+    }
+
+    public static final double L1(Point2D a, Point2D b) {
+        return Math.abs(X(a)-X(b)) + Math.abs(Y(a)-Y(b));
+    }
+
+    public static final boolean collinear(Point2D a, Point2D b, Point2D c) {
+        double x1 = X(a);
+        double y1 = Y(a);
+        double x2 = X(b);
+        double y2 = Y(b);
+        double x3 = X(c);
+        double y3 = Y(c);
+
+        return Math.abs((x2-x1)*(y3-y1)-(y2-y1)*(x3-x1)) < EPSILON;
+    }
+
+    public static boolean epsilonEquals(Point2D a, Point2D b) {
+        return Math.abs(X(a)-X(b)) < EPSILON 
+            && Math.abs(Y(a)-Y(b)) < EPSILON;
+    }
+
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :    

http://dive4elements.wald.intevation.org