diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/geom/VectorUtils.java @ 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
children 281b9430c720
line wrap: on
line diff
--- /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