diff --git a/pkg/utils.go b/pkg/utils.go index 9efda8a..2a96c7d 100644 --- a/pkg/utils.go +++ b/pkg/utils.go @@ -15,3 +15,11 @@ func check(err error) { log.Fatal(err) } } + +type Numeric interface { + float64 +} + +func MapFloatBetween[T Numeric](value, start1, stop1, start2, stop2 T) T { + return start2 + (value-start1)*(stop2-start2)/(stop1-start1) +} diff --git a/pkg/utils_test.go b/pkg/utils_test.go new file mode 100644 index 0000000..a7224ef --- /dev/null +++ b/pkg/utils_test.go @@ -0,0 +1,14 @@ +package gomus_test + +import ( + "testing" + + gomus "git.cesium.pw/niku/gomus/pkg" +) + +func TestMapBetween(t *testing.T) { + r := gomus.MapFloatBetween(2, 1, 3, 5, 10) + if r != 7.5 { + t.Fatalf("Expected value to be mapped to #, got: %f", r) + } +}