core: add Rescale utility function for straight linear rescaling

Signed-off-by: Ron Evans <ron@hybridgroup.com>
This commit is contained in:
Ron Evans 2018-08-15 17:06:52 +02:00
parent 46dab20a2d
commit 355181843f
2 changed files with 10 additions and 0 deletions

View File

@ -57,6 +57,11 @@ func ToScale(input, min, max float64) float64 {
}
}
// Rescale performs a direct linear rescaling of a number from one scale to another.
func Rescale(input, fromMin, fromMax, toMin, toMax float64) float64 {
return (input-fromMin)*(toMax-toMin)/(fromMax-fromMin) + toMin
}
// DefaultName returns a sensible random default name
// for a robot, adaptor or driver
func DefaultName(name string) string {

View File

@ -73,6 +73,11 @@ func TestToScale(t *testing.T) {
gobottest.Assert(t, ToScale(0.5, 0, 10), 5.0)
}
func TestRescale(t *testing.T) {
gobottest.Assert(t, Rescale(500, 0, 1000, 0, 10), 5.0)
gobottest.Assert(t, Rescale(-1.0, -1, 0, 490, 350), 490.0)
}
func TestRand(t *testing.T) {
a := Rand(10000)
b := Rand(10000)