[Dive4elements-commits] [PATCH] LinearInterpolated#apply(): Added simple test if the two datasets intersect at all

Wald Commits scm-commit at wald.intevation.org
Sun Jun 2 18:13:34 CEST 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1370189609 -7200
# Node ID 12af732c9d0fb2d5980958000393c496feda248d
# Parent  0587819960c3e3636adf5aeb040a89b0cae1d3eb
LinearInterpolated#apply(): Added simple test if the two datasets intersect at all.

diff -r 0587819960c3 -r 12af732c9d0f artifacts/src/main/java/org/dive4elements/river/artifacts/model/LinearInterpolated.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/LinearInterpolated.java	Sun Jun 02 17:52:53 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/LinearInterpolated.java	Sun Jun 02 18:13:29 2013 +0200
@@ -176,12 +176,33 @@
         return apply(MAX, other, from, to);
     }
 
+    public boolean intersect(LinearInterpolated other) {
+        if (xs.isEmpty() || other.xs.isEmpty()) {
+            return false;
+        }
+
+        double tMax = xs.max();
+        double oMin = other.xs.min();
+        if (tMax < oMin) {
+            return false;
+        }
+
+        double tMin = xs.min();
+        double oMax = other.xs.max();
+        return !(tMin > oMax);
+    }
+
     public LinearInterpolated apply(
         Operator           operator,
         LinearInterpolated other,
         double             from,
         double             to
     ) {
+        LinearInterpolated result = new LinearInterpolated();
+        if (!intersect(other)) {
+            return result;
+        }
+
         Set<Double> points = new TreeSet<Double>(CMP);
         points.add(from);
         points.add(to);
@@ -189,7 +210,6 @@
         this .pointsInRange(from, to, points);
         other.pointsInRange(from, to, points);
 
-        LinearInterpolated result = new LinearInterpolated();
 
         for (double x: points) {
             if (!inGap(x) && !other.inGap(x)) {


More information about the Dive4elements-commits mailing list