Skip to content

Instantly share code, notes, and snippets.

@didasy
Last active December 2, 2020 09:06
Show Gist options
  • Save didasy/fef10d2d646a87f6d5f908cfea7f4772 to your computer and use it in GitHub Desktop.
Save didasy/fef10d2d646a87f6d5f908cfea7f4772 to your computer and use it in GitHub Desktop.
Remap Range to Another Range
package main
import (
"fmt"
)
func main() {
unmappedValue := 512
minInput := 0
maxInput := 1023
minOutput := 1000
maxOutput := 3000
fmt.Println(remap(unmappedValue, minOutput, maxOutput, minInput, maxInput))
}
func remap(n, toMin, toMax, fromMin, fromMax int) int {
return (toMax - toMin) * (n - fromMin) / (fromMax - fromMin) + toMin
}
@didasy
Copy link
Author

didasy commented Dec 2, 2020

Just change to float if you want float output and input

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment