- Home /
Convert linear range to another range, maintaining ratio, but without knowing the original minimum and maximum
So, I am working with a dataset that contains a variable which can have a value of various ranges that I am unable to predetermine. The range is a value that can range from -x to x, with the median at 0, and x could be a range such as -15 to 15, or it could be -1 to 1. The data I receive is more based on the deviation from 0 than from the range min to the range max, and I believe this is vital to the formula.
I am trying to convert the range of -x to x, to be either in the range of 0 to 100, or -1 to 1, all while maintaining the original ratios. I have taken a look at this stack overflow question: "Convert a number range to another range, maintaining ratio" but the formulas there require a known and predetermined range min and max to start with. Math is certainly not my strong suit and I am at a total loss of how to convert this data, so any help here would be greatly appreciated.
If it does help any, the data I receive is in a list/array, something like:
x[]:{ 1.47f, 1.21f, 0.49f, -0.05f, -0.69f, -1.8f, -2.35f, -1.2f, -0.42f, 4.56f }
My ultimate goal with this is to graph the data onto a UI line graph of size x:200, y:100, with the 0 value being centered on the Y axis, and the graph being plotted over time across the x axis. So, the minimum value should be the lowest value on the graph, being 0 on the y axis, and the maximum value should be 100 on the y axis, with the middle values being plotted between these values while maintaining the ratio between them.
this comment greatly clarifies your question. I'd say you Do know the input range: it's the min/max of the data values. With that in hand, @BastianUrbach 's answer has all you need.
Answer by BastianUrbach · Feb 14 at 08:01 AM
It sounds like you're just looking for Mathf.Lerp and Mathf.InverseLerp (aka linear interpolation and inverse linear interpolation). Lerp maps 0-1 to any other range while InverseLerp maps any range to 0-1. By combining the two, you can easily map any range to any other range:
var t = Mathf.InverseLerp(oldMin, oldMax, value);
value = Mathf.Lerp(newMin, newMax, t);
The math behind it is not too complicated:
Lerp(a, b, t) = (1 - t) * a + t * b
InverseLerp(a, b, v) = (v - a) / (b - a)
IMO the best way to understand why this works is to check the following requirements by inserting the values in the formulas above:
When t = 0, Lerp must be a.
When t = 1, Lerp must be b.
When v = a, InverseLerp must be 0.
When v = b, InverseLerp must be 1.
Edit:
After reading your question again, what exactly do you mean by "without knowing the original minimum and maximum"? You know the values, right? So what's stopping you from determining the minimum and maximum?
var min = Mathf.Min(array);
var max = Mathf.Max(array);
Your answer

Follow this Question
Related Questions
Distance in a Trajectory of a projectile 0 Answers
Physic Material friction mathematical formula 0 Answers
health bar position formula 3 Answers
Math question about random range and fractions 2 Answers
Perlin Noise subtraction formula 0 Answers