Mercurial > dive4elements > river
diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/LinearRemap.java @ 430:7ab81ff32111 2.3
merged flys-artifacts/2.3
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:10 +0200 |
parents | 909196be11a0 |
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 Fri Sep 28 12:14:10 2012 +0200 @@ -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 :