[math] Formula for scaling numbers?
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. This forum is archived - please use this set of forums to ask new questions.
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. This forum is archived - please use this set of forums to ask new questions.
[math] Formula for scaling numbers?
Let's say I want to scale a range of numbers... say, from 0.0 to 1.0 and translate it into another range, say 0 to 320.
How would I do this?
How would I do this?
Well, to convert from one range to another, you'd typically normalize the first range, then multiply the normalized value by the max value of the second range. However, since your first range is 0.0-1.0, it's already normalized. Therefore, all you need to do in this case is multiply the number by 320.
Examples:
0.0 (x 320) = 0
0.5 (x 320) = 160
0.6921 (x 320) = ~221
1.0 (x 320) = 320
(In case you're wondering, normalization is the process of converting a range into a decimal number between 0.0 and 1.0 -- To normalize 0-320, you'd divide each number in the range by 320)
[EDIT]Here's a more robust formula for you:
x = NewMax * n/OldMax
Examples:
0.0 (x 320) = 0
0.5 (x 320) = 160
0.6921 (x 320) = ~221
1.0 (x 320) = 320
(In case you're wondering, normalization is the process of converting a range into a decimal number between 0.0 and 1.0 -- To normalize 0-320, you'd divide each number in the range by 320)
[EDIT]Here's a more robust formula for you:
x = NewMax * n/OldMax
Last edited by HotWax on Fri Apr 27, 2007 9:56 am, edited 1 time in total.
- chopkinsca
- Posts: 1325
- Joined: Thu Dec 11, 2003 5:03 pm
Code: Select all
x = NewMax * n/OldMax
The value to convert. In sticking with the old example, converting from 0-320 to 0.0-1.0:
While there are some obvious shortcuts which could be made in this particular scenario, the point of HotWax's formula is to be general purpose; you should be able to convert from any one range to any other with it.
EDIT: Zero based ranges, that is. If you wanted an arbitrary base, just shift the result value by the minimum (e.g. to convert to a range of 8-18, just convert to range 0-10, then add 8.)
Code: Select all
x = NewMax * n/OldMax
n = 160 //Convert 160 to 0.0-1.0 range
NewMax = 1.0
OldMax = 320
x = 1.0 * 160/320
x = 1.0 * 0.5
x = 0.5
Code: Select all
x = NewMax * n/OldMax
n = 20 //0-32 range, going to new 0-10 range
NewMax = 10
OldMax = 32
x = 10 * 20/32
x = 10 * 0.625
x = 6.25
I guess I could have been more consistent.Nash wrote:What would n be?Code: Select all
x = NewMax * n/OldMax
Here you go:
NewVal = NewMax * OldVal/OldMax
Re: [math] Formula for scaling numbers?
Sorry for the bump, but I need help. I've been trying to pull something off for the last 3 hours but I just can't figure out the math.
I know the value of the player's current EXP, the max EXP, and the EXP required to the next level. Example...
Now using SBARINFO, I'm trying to draw an EXP progress bar similar to Diablo 2. It's basically a percentage bar, from 0 to 100, that shows your experience progress for the current level.
The math that I'm trying to figure out now is, for each new level, how to properly draw this bar. So if the player levels up...
... I want to convert the bar's range (0 to 100) to the range of the EXP to next level (300 in the example's case).
Help. :(
I know the value of the player's current EXP, the max EXP, and the EXP required to the next level. Example...
Code: Select all
EXP: 1/300
EXP to next level: 299 // found by using EXP To Next Level - Current EXP
The math that I'm trying to figure out now is, for each new level, how to properly draw this bar. So if the player levels up...
Code: Select all
EXP: 300/600
EXP to next level: 300
Help. :(
- Project Shadowcat
- Posts: 9369
- Joined: Thu Jul 14, 2005 8:33 pm
- Operating System Version (Optional): Windows 10
- Graphics Processor: nVidia with Vulkan support
- Location: Blacksburg, SC USA
- Contact:
Re: [math] Formula for scaling numbers?
I have a "hack". Use a new ammo bin or a new inventory, and dynamically set the capacity. The joy of the DrawBar is that it updates when it does receive a new capacity.
For each level, empty the bin, set a new capacity, based on ONLY from one level to the next. There is a slight disadvantage, and that is that it will cap off, but a little ACS to correct it shouldn't be much of a problem, right?
For each level, empty the bin, set a new capacity, based on ONLY from one level to the next. There is a slight disadvantage, and that is that it will cap off, but a little ACS to correct it shouldn't be much of a problem, right?
Re: [math] Formula for scaling numbers?
I... still don't get it. Formula, please?
- Project Shadowcat
- Posts: 9369
- Joined: Thu Jul 14, 2005 8:33 pm
- Operating System Version (Optional): Windows 10
- Graphics Processor: nVidia with Vulkan support
- Location: Blacksburg, SC USA
- Contact:
Re: [math] Formula for scaling numbers?
(Total accumulated XP for all previous levels) = Bottom End
(Distance from that point to next) = Top End
It's hard to print a formula from here for me, I've really lost my math edge over the years. However I hope that helps slightly.
(Distance from that point to next) = Top End
It's hard to print a formula from here for me, I've really lost my math edge over the years. However I hope that helps slightly.
- MasterOFDeath
- ... in rememberance ...
- Posts: 2024
- Joined: Sat Apr 03, 2004 10:58 am
Re: [math] Formula for scaling numbers?
HotWax's equation should work fine. I'll try to explain it better --Nash wrote:I... still don't get it. Formula, please?
x = y * n/z
y, or "NewMax" is the new maximum value that you want to range to be. In your case 320.
z, or "OldMax" is the old maximum value of the range. In your case, 1.
n is the value you want to convert. Lets use 0.5 in this example:
x = 320 * 0.5/1
x would equal 160 in this case.
Re: [math] Formula for scaling numbers?
Okay, I know how to retrieve the bottom end, but what I'm having trouble is, how to convert that bottom end to become the new 0? (Because the new range is from 0 to 100 for the percentage bar)Project Dark Fox wrote:(Total accumulated XP for all previous levels) = Bottom End
(Distance from that point to next) = Top End
EDIT for clarity: In my current situation, I have a new minimum and maximum so I don't know how to apply the formula Hotwax gave me.
Example: I want to scale [350 - 800] to [0 - 100]. I want an expression that I can use to do that. Hope my question is clear now...
Re: [math] Formula for scaling numbers?
Anyone? I'm getting really desperate here because I believe this is something really simple to do and I don't need to lose 2 nights of sleep trying to figure this out. :(
There has to be a single expression I can use for this, instead of doing it like how PDF suggested because his way is just too messy and requires a lot of questionable steps...
There has to be a single expression I can use for this, instead of doing it like how PDF suggested because his way is just too messy and requires a lot of questionable steps...