comparison artifacts/src/main/java/org/dive4elements/river/artifacts/math/Linear.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/artifacts/math/Linear.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.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 public static final void weight(
69 double factor,
70 double [] a, double [] b, double [] c
71 ) {
72 int N = Math.min(Math.min(a.length, b.length), c.length);
73 for (int i = 0; i < N; ++i) {
74 c[i] = weight(factor, a[i], b[i]);
75 }
76 }
77 }
78 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org