diff cmd/isoareas/main.go @ 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
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() }()