comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/Linear.java @ 655:913b52064449

Refactored version of "Berechnung 4" flys-artifacts/trunk@2053 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 05 Jun 2011 18:24:46 +0000
parents
children c09c9e05ecfa
comparison
equal deleted inserted replaced
654:bbc966c81809 655:913b52064449
1 package de.intevation.flys.artifacts.math;
2
3 public final class Linear
4 implements Function
5 {
6 private double m;
7 private double b;
8
9 public Linear(
10 double x1, double x2,
11 double y1, double y2
12 ) {
13 // y1 = m*x1 + b
14 // y2 = m*x2 + b
15 // y2 - y1 = m*(x2 - x1)
16 // m = (y2 - y1)/(x2 - x1) # x2 != x1
17 // b = y1 - m*x1
18
19 if (x1 == x2) {
20 m = 0;
21 b = 0.5*(y1 + y2);
22 }
23 else {
24 m = (y2 - y1)/(x2 - x1);
25 b = y1 - m*x1;
26 }
27 }
28
29 public static final double linear(
30 double x,
31 double x1, double x2,
32 double y1, double y2
33 ) {
34 // y1 = m*x1 + b
35 // y2 = m*x2 + b
36 // y2 - y1 = m*(x2 - x1)
37 // m = (y2 - y1)/(x2 - x1) # x2 != x1
38 // b = y1 - m*x1
39
40 if (x1 == x2) {
41 return 0.5*(y1 + y2);
42 }
43 double m = (y2 - y1)/(x2 - x1);
44 double b = y1 - m*x1;
45 return x*m + b;
46 }
47
48 @Override
49 public double value(double x) {
50 return m*x + b;
51 }
52
53 public static final double factor(double x, double p1, double p2) {
54 // 0 = m*p1 + b <=> b = -m*p1
55 // 1 = m*p2 + b
56 // 1 = m*(p2 - p1)
57 // m = 1/(p2 - p1) # p1 != p2
58 // f(x) = x/(p2-p1) - p1/(p2-p1) <=> (x-p1)/(p2-p1)
59
60 return p1 == p2 ? 0.0 : (x-p1)/(p2-p1);
61 }
62
63 public static final double weight(double factor, double a, double b) {
64 //return (1.0-factor)*a + factor*b;
65 return a + factor*(b-a);
66 }
67 }
68 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org