diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/LinearRemap.java @ 427:909196be11a0

First step to calculate "W fuer ungleichwertige Abfluesse" correctly. flys/issue55 flys-artifacts/trunk@1925 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 15 May 2011 21:08:41 +0000
parents
children 343f248e4c8c
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/LinearRemap.java	Sun May 15 21:08:41 2011 +0000
@@ -0,0 +1,78 @@
+package de.intevation.flys.artifacts.math;
+
+public class LinearRemap
+{
+    public static class Segment {
+
+        protected Segment next;
+
+        protected double from;
+        protected double to;
+
+        protected double m;
+        protected double b;
+
+        public Segment() {
+        }
+
+        public Segment(
+            double from, double to, 
+            double m,    double b,
+            Segment next
+        ) {
+            this.from = from;
+            this.to   = to;
+            this.m    = m;
+            this.b    = b;
+        }
+
+        public double eval(double x) {
+            return m*x + b;
+        }
+    } // class Segment
+
+    protected Segment head;
+
+    public LinearRemap() {
+    }
+
+    public void add(
+        double from, double to,
+        double x1,   double y1,
+        double x2,   double y2
+    ) {
+        // y1 = m*x1 + b <=> b = y1 - m*x1
+        // y2 = m*x2 + b
+        // y2 - y1 = m*(x2 - x1)
+        // m = (y2 - y1)/(x2 - x1)
+
+        double m, b;
+
+        if (x2 == x1) {
+            m = 0.0;
+            b = 0.5*(y2 + y1);
+        }
+        else {
+            m = (y2 - y1)/(x2 - x1);
+            b = y1 - m*x1;
+        }
+
+        head = new Segment(from, to, m, b, head);
+    }
+
+    public double eval(double pos, double x) {
+        Segment current = head;
+
+        while (current != null) {
+
+            if (pos >= current.from && pos <= current.to) {
+                return current.eval(x);
+            }
+
+            current = current.next;
+        }
+
+        return Double.NaN;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org