Mercurial > dive4elements > river
diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/Utils.java @ 4259:5cc9453456a7
First complete but untested version of the 'Auslagerung extremer Wasserspiegellagen' calculation.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Oct 2012 17:25:37 +0200 |
parents | |
children | b195fede1c3b |
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/math/Utils.java Thu Oct 25 17:25:37 2012 +0200 @@ -0,0 +1,61 @@ +package de.intevation.flys.artifacts.math; + + +public final class Utils { + + public static final double EPSILON = 1e-3; + + private Utils() { + } + + public static final boolean epsilonEquals(double a, double b) { + return epsilonEquals(a, b, EPSILON); + } + + public static final boolean epsilonEquals(double a, double b, double eps) { + return Math.abs(a - b) < eps; + } + + public static int relativeCCW( + double x1, double y1, + double x2, double y2, + double px, double py + ) { + if ((epsilonEquals(x1, x2) && epsilonEquals(y1, y2)) + || ((epsilonEquals(x1, px) && epsilonEquals(y1, py)))) { + return 0; // Coincident points. + } + // Translate to the origin. + x2 -= x1; + y2 -= y1; + px -= x1; + py -= y1; + double slope2 = y2 / x2; + double slopep = py / px; + if (epsilonEquals(slope2, slopep) + || (epsilonEquals(x2, 0.0) && epsilonEquals(px, 0.0))) { + return y2 > EPSILON // Colinear. + ? (py < -EPSILON ? -1 : py > y2 ? 1 : 0) + : (py > -EPSILON ? -1 : py < y2 ? 1 : 0); + } + if (x2 >= EPSILON && slope2 >= EPSILON) { + return px >= EPSILON // Quadrant 1. + ? (slope2 > slopep ? 1 : -1) + : (slope2 < slopep ? 1 : -1); + } + + if (y2 > EPSILON) { + return px < -EPSILON // Quadrant 2. + ? (slope2 > slopep ? 1 : -1) + : (slope2 < slopep ? 1 : -1); + } + if (slope2 >= EPSILON) { + return px >= EPSILON // Quadrant 3. + ? (slope2 < slopep ? 1 : -1) + : (slope2 > slopep ? 1 : -1); + } + return px < -EPSILON // Quadrant 4. + ? (slope2 < slopep ? 1 : -1) + : (slope2 > slopep ? 1 : -1); + } +}