Page 1 of 2

[math] Formula for scaling numbers?

Posted: Fri Apr 27, 2007 9:30 am
by Nash
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?

Posted: Fri Apr 27, 2007 9:52 am
by HotWax
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

Posted: Fri Apr 27, 2007 9:53 am
by chopkinsca
Divide the new range by the old range and that should work. I'm assuming you want to know this for sethudscale. I'm probably wrong though.

Edit: beaten by a better explanation.

Posted: Fri Apr 27, 2007 9:56 am
by Nash
Specifically, I'm trying to convert the player's angle to a range of numbers to position a mouse cursor.

But I just realized the player's angle doesn't really work the way I thought it should though... hmmm... turning clockwise (to the right) decreases the the value instead of increasing.

Posted: Fri Apr 27, 2007 9:58 am
by HotWax
Nash wrote:But I just realized the player's angle doesn't really work the way I thought it should though... hmmm... turning clockwise (to the right) decreases the the value instead of increasing.
That is correct. [wiki=Definitions]See here for assistance.[/wiki]

Posted: Fri Apr 27, 2007 9:59 am
by Nash

Code: Select all

x = NewMax * n/OldMax
What would n be?

Posted: Fri Apr 27, 2007 11:06 am
by Zippy
The value to convert. In sticking with the old example, converting from 0-320 to 0.0-1.0:

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
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.

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
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.)

Posted: Fri Apr 27, 2007 11:55 am
by HotWax
Nash wrote:

Code: Select all

x = NewMax * n/OldMax
What would n be?
I guess I could have been more consistent. :P

Here you go:

NewVal = NewMax * OldVal/OldMax

Re: [math] Formula for scaling numbers?

Posted: Sat Jul 19, 2008 4:17 pm
by Nash
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...

Code: Select all

EXP: 1/300
EXP to next level: 299 // found by using EXP To Next Level - Current EXP
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...

Code: Select all

EXP: 300/600
EXP to next level: 300
... 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. :(

Re: [math] Formula for scaling numbers?

Posted: Sat Jul 19, 2008 8:38 pm
by Project Shadowcat
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?

Re: [math] Formula for scaling numbers?

Posted: Sat Jul 19, 2008 8:45 pm
by Nash
I... still don't get it. Formula, please?

Re: [math] Formula for scaling numbers?

Posted: Sat Jul 19, 2008 9:31 pm
by Project Shadowcat
(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.

Re: [math] Formula for scaling numbers?

Posted: Sat Jul 19, 2008 10:13 pm
by MasterOFDeath
Nash wrote:I... still don't get it. Formula, please?
HotWax's equation should work fine. I'll try to explain it better --

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?

Posted: Sun Jul 20, 2008 4:16 am
by Nash
Project Dark Fox wrote:(Total accumulated XP for all previous levels) = Bottom End

(Distance from that point to next) = Top End
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)

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?

Posted: Sun Jul 20, 2008 1:07 pm
by Nash
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...