Mercurial > gemma
annotate pkg/common/linear.go @ 4784:ecbb12a6e8ee
Added tag v4.2 for changeset fc082c611b8b
author | Sascha Wilde <wilde@intevation.de> |
---|---|
date | Tue, 22 Oct 2019 17:56:53 +0200 |
parents | 07f632cd2625 |
children | 18d5461bec5d |
rev | line source |
---|---|
4558
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 // without warranty, see README.md and license for details. |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 // |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 // |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 // Copyright (C) 2019 by via donau |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 // Software engineering by Intevation GmbH |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 // |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 // Author(s): |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 package common |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 // Linear constructs a function which maps x1 to y1 and x2 to y2. |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 // All other values are interpolated linearly. |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
17 func Linear(x1, y1, x2, y2 float64) func(float64) float64 { |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 // f(x1) = y1 |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 // f(x2) = y2 |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 // y1 = x1*a + b <=> b = y1 - x1*a |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 // y2 = x2*a + b |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 // y1 - y2 = a*(x1 - x2) |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
24 // a = (y1-y2)/(x1 - x2) for x1 != x2 |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 if x1 == x2 { |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 return func(float64) float64 { |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 return 0.5 * (y1 + y2) |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 } |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 } |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
31 a := (y1 - y2) / (x1 - x2) |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 b := y1 - x1*a |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 return func(x float64) float64 { |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 return x*a + b |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
35 } |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 } |