[math] Formula for scaling numbers?

Discuss all aspects of editing for ZDoom.
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

[math] Formula for scaling numbers?

Postby Nash » Fri Apr 27, 2007 10:30 am

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
Nash
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia

Postby HotWax » Fri Apr 27, 2007 10:52 am

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 10:56 am, edited 1 time in total.
User avatar
HotWax
Do what you must, and pay the price later.
 
Joined: 18 Jul 2003
Location: Idaho Falls, ID

Postby chopkinsca » Fri Apr 27, 2007 10:53 am

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
chopkinsca
"." is too short
 
Joined: 11 Dec 2003

Postby Nash » Fri Apr 27, 2007 10:56 am

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
Nash
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia

Postby HotWax » Fri Apr 27, 2007 10:58 am

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. See here for assistance.
User avatar
HotWax
Do what you must, and pay the price later.
 
Joined: 18 Jul 2003
Location: Idaho Falls, ID

Postby Nash » Fri Apr 27, 2007 10:59 am

Code: Select allExpand view
x = NewMax * n/OldMax


What would n be?
User avatar
Nash
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia

Postby Zippy » Fri Apr 27, 2007 12:06 pm

The value to convert. In sticking with the old example, converting from 0-320 to 0.0-1.0:
Code: Select allExpand view
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 allExpand view
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
Zippy
Scathing political commentary!
 
Joined: 23 Mar 2005
Location: Rochester

Postby HotWax » Fri Apr 27, 2007 12:55 pm

Nash wrote:
Code: Select allExpand view
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
HotWax
Do what you must, and pay the price later.
 
Joined: 18 Jul 2003
Location: Idaho Falls, ID

Re: [math] Formula for scaling numbers?

Postby Nash » Sat Jul 19, 2008 5:17 pm

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 allExpand view
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 allExpand view
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
Nash
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia

Re: [math] Formula for scaling numbers?

Postby Project Dark Fox » Sat Jul 19, 2008 9:38 pm

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
Project Dark Fox
Learn Toaster Repair (Nightmare Fuel Station Attendant)
 
Joined: 14 Jul 2005
Location: The Gutter

Re: [math] Formula for scaling numbers?

Postby Nash » Sat Jul 19, 2008 9:45 pm

I... still don't get it. Formula, please?
User avatar
Nash
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia

Re: [math] Formula for scaling numbers?

Postby Project Dark Fox » Sat Jul 19, 2008 10:31 pm

(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
Project Dark Fox
Learn Toaster Repair (Nightmare Fuel Station Attendant)
 
Joined: 14 Jul 2005
Location: The Gutter

Re: [math] Formula for scaling numbers?

Postby MasterOFDeath » Sat Jul 19, 2008 11:13 pm

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
MasterOFDeath
 
Joined: 03 Apr 2004

Re: [math] Formula for scaling numbers?

Postby Nash » Sun Jul 20, 2008 5:16 am

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
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia

Re: [math] Formula for scaling numbers?

Postby Nash » Sun Jul 20, 2008 2:07 pm

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...
User avatar
Nash
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia

Next

Return to Editing

Who is online

Users browsing this forum: No registered users and 2 guests