daveycroqet daveycroqet - 4 months ago 20
Javascript Question

Math: Formula to scale a number to another number

I asked a similar question earlier today, and it turns out that I just suck at math, because I can't figure this one out, either.

I'm calculating the screen ratio via width/height. I need a function to convert that resulting number to a new scale.


function convertNum(ratio) {
return //formula here


Given a resolution of 3000x1000 = ratio of 3 (i.e. 3000/1000).
I want it converted to 133.3 via the function, e.g. convertNum(3) spits out 133.33

2500x1000 = 2.5 (desired result: 100)
2000x1000 = 2 (desired result: 66.6)
1500x1000 = 1.5 (desired result: 33.3)
1000x1000 = 1 (desired result: 0)

It should keep scaling this way for all screen ratios above 1.0.

Answer Source

You need to add an additional 33.3% for every 0.5 in the ratio.

First figure out how many "padding pieces" you need to add:

# Subtracting 1 since 1 should result in a 0
(ratio - 1) / 0.5

Then multiply the number of padding pieces by the padding amount:

((ratio - 1) / 0.5) * 0.333

This might be able to be simplified further, but my brains fried from working a night shift.