comparison 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
comparison
equal deleted inserted replaced
1796:ae6ace900c07 1797:5eec623db50a
1 package de.intevation.flys.geom;
2
3 import java.awt.geom.Point2D;
4
5 public final class VectorUtils
6 {
7 public static final double EPSILON = 1e-4;
8
9 private VectorUtils() {
10 }
11
12 private static final double X(Point2D p) {
13 return p.getX();
14 }
15
16 private static final double Y(Point2D p) {
17 return p.getY();
18 }
19
20 public static final Point2D sub(Point2D a, Point2D b) {
21 return new Point2D.Double(X(a)-X(b), Y(a)-Y(b));
22 }
23
24 public static final double dot(Point2D a, Point2D b) {
25 return X(a)*X(b) + Y(a)*Y(b);
26 }
27
28 public static final Point2D add(Point2D a, Point2D b) {
29 return new Point2D.Double(X(a)+X(b), Y(a)+Y(b));
30 }
31
32 public static final Point2D negate(Point2D a) {
33 return new Point2D.Double(-X(a), -Y(a));
34 }
35
36 public static final Point2D ortho(Point2D a) {
37 return new Point2D.Double(-Y(a), X(a));
38 }
39
40 public static final Point2D scale(Point2D a, double s) {
41 return new Point2D.Double(s*X(a), s*Y(a));
42 }
43
44 public static final double lengthSq(Point2D a) {
45 double x = X(a);
46 double y = Y(a);
47 return x*x + y*y;
48 }
49
50 public static final double length(Point2D a) {
51 return Math.sqrt(lengthSq(a));
52 }
53
54 public static final Point2D normalize(Point2D a) {
55 double length = length(a);
56 return length != 0d
57 ? scale(a, 1d/length)
58 : new Point2D.Double(X(a), Y(a));
59 }
60
61 public static final double L1(Point2D a, Point2D b) {
62 return Math.abs(X(a)-X(b)) + Math.abs(Y(a)-Y(b));
63 }
64
65 public static final boolean collinear(Point2D a, Point2D b, Point2D c) {
66 double x1 = X(a);
67 double y1 = Y(a);
68 double x2 = X(b);
69 double y2 = Y(b);
70 double x3 = X(c);
71 double y3 = Y(c);
72
73 return Math.abs((x2-x1)*(y3-y1)-(y2-y1)*(x3-x1)) < EPSILON;
74 }
75
76 public static boolean epsilonEquals(Point2D a, Point2D b) {
77 return Math.abs(X(a)-X(b)) < EPSILON
78 && Math.abs(Y(a)-Y(b)) < EPSILON;
79 }
80
81 }
82 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org