First implementation

This commit is contained in:
2019-12-27 18:23:08 +01:00
parent 1cf57702cc
commit b3de5ca724
211 changed files with 28458 additions and 0 deletions

12
util/util.go Normal file
View File

@ -0,0 +1,12 @@
package util
// Linear mapping between two ranges of values
func MapRange(x, xmin, xmax, ymin, ymax float64) int {
Xrange := xmax - xmin
Yrange := ymax - ymin
XYratio := Xrange / Yrange
y := (x-xmin)/XYratio + ymin
return int(y)
}

32
util/util_test.go Normal file
View File

@ -0,0 +1,32 @@
package util
import "testing"
func TestRangeMapping(t *testing.T) {
cases := []struct {
x, xmin, xmax, ymin, ymax float64
expected int
}{
// Test positive
{-100, -100, 100, 0, 1000, 0},
{0, -100, 100, 0, 1000, 500},
{100, -100, 100, 0, 1000, 1000},
// Test negative
{0, 0, 100, 0, 1000, 0},
{50, 0, 100, 0, 1000, 500},
{100, 0, 100, 0, 1000, 1000},
// Reverse
{0, 100, 0, 0, 1000, 1000},
{50, 100, 0, 0, 1000, 500},
{100, 100, 0, 0, 1000, 0},
}
for _, c := range cases {
val := MapRange(c.x, c.xmin, c.xmax, c.ymin, c.ymax)
if val != c.expected {
t.Errorf("MapRange(%.0f, %.0f, %.0f, %.0f, %.0f): %d, wants %d", c.x, c.xmin, c.xmax, c.ymin, c.ymax, val, c.expected)
}
}
}