Skip to content

Commit

Permalink
core: add Rescale utility function for straight linear rescaling
Browse files Browse the repository at this point in the history
Signed-off-by: Ron Evans <[email protected]>
  • Loading branch information
deadprogram committed Aug 15, 2018
1 parent 46dab20 commit 3551818
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3551818

Please sign in to comment.