# Algebra math problem in a Program. I need help with a formula? • ### Question

•  0

Hi
VB2008 express Visual basic language.

This is what I want to happen.
The app uses two progressbars one is labeled en = energy level and the second is al = alcohol level
As the alcohol level increases. The amount deducted from en decreases and allows longer game play before
having to purchase food or drink. If en = 0 game ends. So with the above given I believe I need the
following variables to caculate for r = en reduction rate. Where the range min & max is values that determine
the range of r. Also the maximum that r value can be is .99 and the minimum that r value can be is .1.
If anyone sees a way to do this using more variables or a simpler method it would be most welcome.I can add
to the program as many variables as needed such as multipliers.

Rmx = range maximum preset  by program selection for easy, medimum, hard
Rmn = range minimum preset  by program selection for easy, medimum, hard
al = variable from 0 - 1000
al has to be reversed with this added into the formula
result = 1000 - al (al now is 1000 to 0) (this so when the vlaue of progressbar is high it will give lower reduction
rate and when it is low it will give higher reduction rate)
r = the amount deducted from en level that sets the amount of real time versis game time

All of the above is needed because of game design. At night food purchase is very restricted and alcohol is
about all that can be purchased because the only thing open is a bar.

I want to be able to decrease the amount being deducted from energy by using the alcohol level. As the
alcohol level increases I want the rate of deduction to decrease so that alcohol takes the primary role in the
in detemination as to how long the time period before energy has to be refilled.

sorry if this is a little confusing Scaling from one input to an output and a fractional decimal at that is difficult.

Curtis

Always Lost in Code,
Saturday, January 30, 2010 1:38 PM

• Hi everyone after trying for a week to come up with the math formula. I found mynote I made a long time ago on linear equations.
Below are the results from my notes and a salution I came up with for taking any size input and converting it on a linear scale to make the scaled value fall between a min and max value.

equations used in calculating a linear relationship:

Scaled value = (input value x rate) + offset

Rate = (scaled max. - scaled min.) / (input max. - input min.)

Offset = scaled minimum - (input minimum x rate)

Translation to needed formula

Original            new        description

Scaled value =  r      =(rate of deduction)
scaled max.  =  rmax   =(r maximum scale)
scaled min.  =  rmim   =(r minimum scale)
Input Max    =  Imax   =( Maximum value of al)
Input Min    =  Imin   =( minimum value of al)
Rate         =  Rate1  =(calculated Value)
offset       =  offset =(calculated value)

err = ( al X rate1 ) + offset
rate1 = ( rmax - rmin ) / ( Imax - Imin )
offset = rmin - ( Imin X rate )

err = (al * (rmax - rmin) / (Imax - Imin)) + (rmin - (Imin * ((rmax - rmin) / (Imax - Imin))))

This formula is a reverse slope. Below is an example.
rmax = .5
rmin  = .3
Imax = 1000
Imin  = 1
en    = 1000
al     = 2 to 1000
If the parameters are set as above then running the above formula will result in the following input(al)  to output(rate1)

al = 1000  then  Rate1 = .300
al =  500   then  Rate1 = .400
al = 1       then  Rate1 = .500

This is using math.round (rate1,3) as you can see the input to output is reversed a larger number produces a smaller output.
It is also linear from input to output. One thing is you can input numbers larger or smaller than the max,min settings and unpredictable results will result.

Hope this helps anyone having a simular problem.

Curtis

Always Lost in Code,
• Marked as answer by Sunday, January 31, 2010 2:06 AM
Sunday, January 31, 2010 2:03 AM