Mercurial > dive4elements > river
comparison 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 |
comparison
equal
deleted
inserted
replaced
426:c6b7ca0febcb | 427:909196be11a0 |
---|---|
1 package de.intevation.flys.artifacts.math; | |
2 | |
3 public class LinearRemap | |
4 { | |
5 public static class Segment { | |
6 | |
7 protected Segment next; | |
8 | |
9 protected double from; | |
10 protected double to; | |
11 | |
12 protected double m; | |
13 protected double b; | |
14 | |
15 public Segment() { | |
16 } | |
17 | |
18 public Segment( | |
19 double from, double to, | |
20 double m, double b, | |
21 Segment next | |
22 ) { | |
23 this.from = from; | |
24 this.to = to; | |
25 this.m = m; | |
26 this.b = b; | |
27 } | |
28 | |
29 public double eval(double x) { | |
30 return m*x + b; | |
31 } | |
32 } // class Segment | |
33 | |
34 protected Segment head; | |
35 | |
36 public LinearRemap() { | |
37 } | |
38 | |
39 public void add( | |
40 double from, double to, | |
41 double x1, double y1, | |
42 double x2, double y2 | |
43 ) { | |
44 // y1 = m*x1 + b <=> b = y1 - m*x1 | |
45 // y2 = m*x2 + b | |
46 // y2 - y1 = m*(x2 - x1) | |
47 // m = (y2 - y1)/(x2 - x1) | |
48 | |
49 double m, b; | |
50 | |
51 if (x2 == x1) { | |
52 m = 0.0; | |
53 b = 0.5*(y2 + y1); | |
54 } | |
55 else { | |
56 m = (y2 - y1)/(x2 - x1); | |
57 b = y1 - m*x1; | |
58 } | |
59 | |
60 head = new Segment(from, to, m, b, head); | |
61 } | |
62 | |
63 public double eval(double pos, double x) { | |
64 Segment current = head; | |
65 | |
66 while (current != null) { | |
67 | |
68 if (pos >= current.from && pos <= current.to) { | |
69 return current.eval(x); | |
70 } | |
71 | |
72 current = current.next; | |
73 } | |
74 | |
75 return Double.NaN; | |
76 } | |
77 } | |
78 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |