comparison flys-artifacts/src/main/java/org/dive4elements/river/artifacts/math/Utils.java @ 5831:bd047b71ab37

Repaired internal references
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:06:39 +0200
parents flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/Utils.java@b195fede1c3b
children
comparison
equal deleted inserted replaced
5830:160f53ee0870 5831:bd047b71ab37
1 package org.dive4elements.river.artifacts.math;
2
3
4 public final class Utils {
5
6 public static final double EPSILON = 1e-3;
7
8 private Utils() {
9 }
10
11 public static final boolean epsilonEquals(double a, double b) {
12 return epsilonEquals(a, b, EPSILON);
13 }
14
15 public static final boolean epsilonEquals(double a, double b, double eps) {
16 return Math.abs(a - b) < eps;
17 }
18
19 public static int relativeCCW(
20 double x1, double y1,
21 double x2, double y2,
22 double px, double py
23 ) {
24 if ((epsilonEquals(x1, x2) && epsilonEquals(y1, y2))
25 || ((epsilonEquals(x1, px) && epsilonEquals(y1, py)))) {
26 return 0; // Coincident points.
27 }
28 // Translate to the origin.
29 x2 -= x1;
30 y2 -= y1;
31 px -= x1;
32 py -= y1;
33 double slope2 = y2 / x2;
34 double slopep = py / px;
35 if (epsilonEquals(slope2, slopep)
36 || (epsilonEquals(x2, 0.0) && epsilonEquals(px, 0.0))) {
37 return y2 > EPSILON // Colinear.
38 ? (py < -EPSILON ? -1 : py > y2 ? 1 : 0)
39 : (py > -EPSILON ? -1 : py < y2 ? 1 : 0);
40 }
41 if (x2 >= EPSILON && slope2 >= EPSILON) {
42 return px >= EPSILON // Quadrant 1.
43 ? (slope2 > slopep ? 1 : -1)
44 : (slope2 < slopep ? 1 : -1);
45 }
46
47 if (y2 > EPSILON) {
48 return px < -EPSILON // Quadrant 2.
49 ? (slope2 > slopep ? 1 : -1)
50 : (slope2 < slopep ? 1 : -1);
51 }
52 if (slope2 >= EPSILON) {
53 return px >= EPSILON // Quadrant 3.
54 ? (slope2 < slopep ? 1 : -1)
55 : (slope2 > slopep ? 1 : -1);
56 }
57 return px < -EPSILON // Quadrant 4.
58 ? (slope2 < slopep ? 1 : -1)
59 : (slope2 > slopep ? 1 : -1);
60 }
61 }

http://dive4elements.wald.intevation.org