changeset 4558:07f632cd2625 iso-areas

Moved linear interpolation to common package for re-use.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 02 Oct 2019 11:00:09 +0200
parents 17cba8b447a6
children 92515c49c566
files cmd/isoareas/main.go pkg/common/linear.go
diffstat 2 files changed, 39 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/cmd/isoareas/main.go	Tue Oct 01 17:39:58 2019 +0200
+++ b/cmd/isoareas/main.go	Wed Oct 02 11:00:09 2019 +0200
@@ -30,6 +30,7 @@
 	svg "github.com/ajstarks/svgo"
 	"github.com/fogleman/contourmap"
 
+	"gemma.intevation.de/gemma/pkg/common"
 	"gemma.intevation.de/gemma/pkg/models"
 	"gemma.intevation.de/gemma/pkg/octree"
 )
@@ -76,27 +77,6 @@
 	}
 }
 
-func linear(x1, y1, x2, y2 float64) func(float64) float64 {
-	// f(x1) = y1
-	// f(x2) = y2
-	// y1 = x1*a + b <=> b = y1 - x1*a
-	// y2 = x2*a + b
-
-	// y1 - y2 = a*(x1 - x2)
-	// a = (y1-y2)/(x1 - x2) for x1 != x2
-
-	if x1 == x2 {
-		return func(float64) float64 {
-			return 0.5 * (y1 + y2)
-		}
-	}
-	a := (y1 - y2) / (x1 - x2)
-	b := y1 - x1*a
-	return func(x float64) float64 {
-		return x*a + b
-	}
-}
-
 func dumpContoursSVG(
 	w io.Writer,
 	heights []float64,
@@ -136,8 +116,8 @@
 	const width = 50000
 	height := int(math.Ceil(width * ratio))
 
-	px := linear(minX, 0, maxX, width)
-	py := linear(minY, float64(height), maxY, 0)
+	px := common.Linear(minX, 0, maxX, width)
+	py := common.Linear(minY, float64(height), maxY, 0)
 
 	out := bufio.NewWriter(w)
 	defer func() { err = out.Flush() }()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/common/linear.go	Wed Oct 02 11:00:09 2019 +0200
@@ -0,0 +1,36 @@
+// This is Free Software under GNU Affero General Public License v >= 3.0
+// without warranty, see README.md and license for details.
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// License-Filename: LICENSES/AGPL-3.0.txt
+//
+// Copyright (C) 2019 by via donau
+//   – Österreichische Wasserstraßen-Gesellschaft mbH
+// Software engineering by Intevation GmbH
+//
+// Author(s):
+//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
+package common
+
+// Linear constructs a function which maps x1 to y1 and x2 to y2.
+// All other values are interpolated linearly.
+func Linear(x1, y1, x2, y2 float64) func(float64) float64 {
+	// f(x1) = y1
+	// f(x2) = y2
+	// y1 = x1*a + b <=> b = y1 - x1*a
+	// y2 = x2*a + b
+
+	// y1 - y2 = a*(x1 - x2)
+	// a = (y1-y2)/(x1 - x2) for x1 != x2
+
+	if x1 == x2 {
+		return func(float64) float64 {
+			return 0.5 * (y1 + y2)
+		}
+	}
+	a := (y1 - y2) / (x1 - x2)
+	b := y1 - x1*a
+	return func(x float64) float64 {
+		return x*a + b
+	}
+}