Skip to content

Commit

Permalink
Add scale functions
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Mar 27, 2014
1 parent bcca7f9 commit a2c9fe6
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"github.com/tarm/goserial"
"io"
"math"
"math/rand"
"net"
"reflect"
Expand Down Expand Up @@ -100,3 +101,18 @@ func toJson(obj interface{}) string {
b, _ := json.Marshal(obj)
return string(b)
}

func FromScale(input, min, max float64) float64 {
return (input - math.Min(min, max)) / (math.Max(min, max) - math.Min(min, max))
}

func ToScale(input, min, max float64) float64 {
i := input*(math.Max(min, max)-math.Min(min, max)) + math.Min(min, max)
if i < math.Min(min, max) {
return math.Min(min, max)
} else if i > math.Max(min, max) {
return math.Max(min, max)
} else {
return i
}
}

0 comments on commit a2c9fe6

Please sign in to comment.