Skip to content

Instantly share code, notes, and snippets.

@shaunsales
Last active June 6, 2025 19:28
Show Gist options
  • Select an option

  • Save shaunsales/8342c8635dfd6091042c to your computer and use it in GitHub Desktop.

Select an option

Save shaunsales/8342c8635dfd6091042c to your computer and use it in GitHub Desktop.
Linear Remap - map the value of one range to another range
// Remap the value of a range to the equivalent in another range
// Y = (X-A)/(B-A) * (D-C) + C
// Use floats because of the percentage significand
float rangeOneStart = 500, rangeOneEnd = 1000;
float rangeTwoStart = 0.5f, rangeTwoEnd = 2.0f;
// Choose an arbitrary number within range one
float numberInRangeOne = 750;
// Convert the number in range one into a percentage
// Reduce the both the "number" and the "end" by the "start" to have a zero-based calculation
float percentOfRangeOne = (numberInRangeOne - rangeOneStart) / (rangeOneEnd - rangeOneStart);
// Reduce the second range to a zero base, multiply by our first range coefficient and then add then increase it with the start value
float percentOfRangeTwo = percentOfRangeOne * (rangeTwoEnd - rangeTwoStart) + rangeTwoStart;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment