jmasterx
jmasterx

Reputation: 54113

Solving simultaneous equations

Here is my problem:

Given x, y, z and ratio where z is known and ratio is known and is a float representing a relative value, I need to find x and y.

I know that:

x / y == ratio
y - x == z

What I'm trying to do is make my own scroll pane and I'm figuring out the scrollbar parameters.

So for example,

If the scrollbar must be able to scroll 100 values (z) and the thumb must consume 80% of the bar (ratio = 0.8) then x would be 400 and y would be 500.

Thanks

Upvotes: 0

Views: 1074

Answers (4)

moggi
moggi

Reputation: 1486

you have to use just a bit math.

x=ratio*y
y=z/(1-ratio)

So you can just calculate y and than x

Upvotes: 0

moinudin
moinudin

Reputation: 138357

From your first equation:

x / y = ratio
=> x = y.ratio

From your second equation:

y - x = z
=> y - y.ratio = z
=> y = z / (1 - ratio)

Plugging in x = y.ratio:

=> x = z.ratio / (1 - ratio)

So you can calculate x = z * ratio / (1 - ratio) and y = z / (1 - ratio). For your example, x = 100 * 0.8 / (1 - 0.8) = 400 and y = 100 / (1 - 0.8) = 500.

Upvotes: 0

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272517

You have two equations in two unknowns. To solve, you need to eliminate one of the unknowns by substitution. For example, we can eliminate y by rearranging the first equation as:

y = x / ratio

and then substituting into the second:

(x / ratio) - x = z

This new equation can then be rearranged in terms of x:

x = z . ratio / (1-ratio)

This then gives you y:

y = z / (1-ratio)

Upvotes: 0

chrisaycock
chrisaycock

Reputation: 37930

From algebra:

y := z / (1 - ratio)
x := y - z

Using your example:

y := 100 / (1 - 0.8) = 100 / 0.2 = 500
x := 500 - 100 = 400

Upvotes: 1

Related Questions