Added MapFloatBetween util-function

This commit is contained in:
strNophix 2022-03-22 00:21:08 +01:00
parent 31e6b1cb5e
commit 8eb850d9fe
2 changed files with 22 additions and 0 deletions

View File

@ -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)
}

14
pkg/utils_test.go Normal file
View File

@ -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)
}
}