Mercurial > gemma
annotate pkg/common/linear.go @ 4855:01b593ea2311
Added missing doc strings for the STR tree in the mesh package.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 19 Nov 2019 17:48:02 +0100 |
parents | 18d5461bec5d |
children |
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> |
4850
18d5461bec5d
Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4558
diff
changeset
|
13 |
4558
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 package common |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 // 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
|
17 // 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
|
18 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
|
19 // f(x1) = y1 |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 // f(x2) = y2 |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 // 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
|
22 // y2 = x2*a + b |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
24 // 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
|
25 // 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
|
26 |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 if x1 == x2 { |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 return func(float64) float64 { |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 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
|
30 } |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
31 } |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 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
|
33 b := y1 - x1*a |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 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
|
35 return x*a + b |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 } |
07f632cd2625
Moved linear interpolation to common package for re-use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 } |