[math] Formula for scaling numbers?

Archive of the old editing forum
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.
User avatar
Nash
 
 
Posts: 17434
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

[math] Formula for scaling numbers?

Post 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?
User avatar
HotWax
Posts: 10002
Joined: Fri Jul 18, 2003 6:18 pm
Location: Idaho Falls, ID

Post 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
Last edited by HotWax on Fri Apr 27, 2007 9:56 am, edited 1 time in total.
User avatar
chopkinsca
Posts: 1325
Joined: Thu Dec 11, 2003 5:03 pm

Post 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.
User avatar
Nash
 
 
Posts: 17434
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Post 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.
User avatar
HotWax
Posts: 10002
Joined: Fri Jul 18, 2003 6:18 pm
Location: Idaho Falls, ID

Post 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]
User avatar
Nash
 
 
Posts: 17434
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Post by Nash »

Code: Select all

x = NewMax * n/OldMax
What would n be?
User avatar
Zippy
Posts: 3302
Joined: Wed Mar 23, 2005 5:31 pm
Location: New Jersey

Post 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.)
User avatar
HotWax
Posts: 10002
Joined: Fri Jul 18, 2003 6:18 pm
Location: Idaho Falls, ID

Post 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
User avatar
Nash
 
 
Posts: 17434
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: [math] Formula for scaling numbers?

Post 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. :(
User avatar
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?

Post 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?
User avatar
Nash
 
 
Posts: 17434
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: [math] Formula for scaling numbers?

Post by Nash »

I... still don't get it. Formula, please?
User avatar
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?

Post 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.
User avatar
MasterOFDeath
... in rememberance ...
Posts: 2024
Joined: Sat Apr 03, 2004 10:58 am

Re: [math] Formula for scaling numbers?

Post 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.
User avatar
Nash
 
 
Posts: 17434
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: [math] Formula for scaling numbers?

Post 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...
User avatar
Nash
 
 
Posts: 17434
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: [math] Formula for scaling numbers?

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

Return to “Editing (Archive)”