Last active
December 2, 2020 09:06
-
-
Save didasy/fef10d2d646a87f6d5f908cfea7f4772 to your computer and use it in GitHub Desktop.
Remap Range to Another Range
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just change to float if you want float output and input