Home > Enterprise >  How to cut down a sum of variables, while keeping the scale between the variable
How to cut down a sum of variables, while keeping the scale between the variable

Time:10-26

This question is actually still about programming, it's just that I can't put it into words (even in my native language) because of that, I will use a story styled question:

Bob has a rope that is made out of 3 ropes: red rope, green rope, and blue rope. The total length of the rope is 150cm, with the red rope 75cm, the green rope 50cm, and the blue rope 25cm. Bob wants to cut down the rope so the total length is 100cm, but without changing the scale between the 3 ropes (3:2:1). What formula can Bob use?

Basically, I'm asking the formula to how to do this, but I can't explain the problem directly so I used a story-based question just like from school. Thank you and sorry!

CodePudding user response:

Let R0 be the original length of the red rope.

Let G0 be the original length of the green rope.

Let B0 be the original length of the blue rope.

Let T0 be the original total length of the rope: T0 = R0 G0 B0.

Let T1 be the required final length of the rope.

The scale by which to modify T0 is therefore S = T1/T0.

Thus T1 = S * T0

Therefore T1 = S * (R0 G0 B0)

And so T1 = S.R0 S.G0 S.B0.

So the new red length = R1 = S.R0

Similarly for G1 = S.G0 and B1 = S.B0

So you just need to compute the scaling factor T1/T0 and multiply each old length by the scale to compute the new lengths.

For your specific example,

S = 100/150 = 2/3.

R0 = 75

G0 = 50

B0 = 25

So R1 = 75 * 2/3 = 50

And G1 = 50 * 2/3 = 33 1/3

And B1 = 25 * 2/3 = 16 2/3

R1 G1 B1 = 50 33 1/3 16 2/3 = 100

  • Related