little formula to solve

BlitzMax Forums/BlitzMax Programming/little formula to solve

Robert Cummings(Posted 2005) [#1]
Hi lads, got a method here for my gui slider. Basically we have these numbers to work with:

- The Range of the slider.

These two variables could be between -1 to 1 or even 0 to 10. This is range1 and range2. They dictate the range of values the slider can have.

- The Value of the slider. This variable is a value between the range above.


What I want to do is use the two range variables and the value between that range to calculate a final percentage between 0 and 99. How do I do it?


Tobs(Posted 2005) [#2]
Percentage = ((Value - Range1) * 100) / (Range2 - Range1)


deps(Posted 2005) [#3]
Maybe something like this?
r = range2-range1
p = ((value-range1)/r)*99

p should now contain a value between 0 and 99

Not 100% sure it works. Didn't test it and I'm really tierd. I also have a cold wich makes it hard to think, sometimes. :P

Edity: I was also too slow writing this post.
2nd edit: Heh, removed a small typo in the code. :)


Tobs(Posted 2005) [#4]
Haha! I saw that first version :)


TartanTangerine (was Indiepath)(Posted 2005) [#5]
Hic


Robert Cummings(Posted 2005) [#6]
The reason why the formulas above don't work is because they don't deal with a situation like -1 to 1 where the value is actually 0...

The problem is when you have a range of -1 to 1 and the value is 0... (in the middle) you multiply that by 100 and get 0 instead of the correct value of 50

Now can you see the problem with it?


Tobs(Posted 2005) [#7]
Mine and Deps do work. Mine is slightly better since it would still work well with Ints.

Your example:

Percentage = ((Value - Range1) * 100) / (Range2 - Range1)
Percentage = (( 0 - -1 ) * 100) / ( 1 - -1 )
Percentage = (( 1 ) * 100) / ( 2 )
Percentage = 100 / 2
Percentage = 50


TomToad(Posted 2005) [#8]
Offset = 0 - Range1
SValue = Value + Offset
Percent = SValue * 100 / (Range2 - Range1)

I think will work.


Tobs(Posted 2005) [#9]
That does exactly the same:

SValue is the same as (Value - Range1)


TartanTangerine (was Indiepath)(Posted 2005) [#10]
Ha, shaken off the hangover, here you are :

Two functions, One will return the value at a given percentage of the slider. The other will return the percentage value of the slider for a given value.
For Local a = 0 To 99
	Print SliderValue(-2.0,4.0,a)
Next


Function SliderValue:Float(Range1:Float,Range2:Float,SliderValue:Float)
	Return (SliderValue * ((Range2 - Range1) / 100)) + Range1
End Function

Function SliderPercentage:Float(Range1:Float,Range2:Float,ActualValue:Float)
	Return  (ActualValue - Range1) / ((Range2 - Range1) / 100)
End Function



Robert Cummings(Posted 2005) [#11]
Thanks a lot :D

Much appreciated everyone.