This topic is 882 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

I have X amount of variables, they all need to add up to an amount (100). There are some conditions that must be met.

• When I set one variable, the others need to reduce/increase.
• the variables can not go less than 0.
• The variables must stay at the same ratio, unless one or more is decreased to 0

Currently I have an issue where I will have a set of varibles: 1, 50, 25,24 . And I want to increase the first variable to 91.

 for (int i = 0; i < arraySize; i++)
{
int SliderValue = aarray[i]
if (SliderValue!= oldValue) //only update when changed
{
int difference = SliderValue - oldValue;//how much did the slider change by
oldValue = SliderValue; //update the old value

int amount = difference / arraySize - 1);
int remainder = 0;
if (difference % arraySize - 1) != 0)//We need to know if it has rounded down
{
remainder = difference % (arraySize -1);

}

for (int j = 0; j < arraySize; j++) //Change the other values
{
if (j == i)//dont change youself
continue;

int b = array[j];
if (remainder != 0 && (b>= 0 && b <= goal))
{
if (remainder < 0)
{
increaselowest(array, /*element to ignore*/i, Mathf.Abs(remainder));

}
else
decreaselargest(array, /*element to ignore*/i, Mathf.Abs(remainder));

remainder = 0;

}

b= b- amount;
Oldvalue= b.intValue;
}

}


apologies if there are bugs/errors in this code. It was written using Unity serialization stuff, and had to edit it down.

increaselowest will find the lowest variable in the array and increase it by the amount.

DecreaseLargest will find the largest value and decrease it by an amount.

My problem comes when I increase by a large amount. with the numbers 1, 50, 25,24. and increase the first variable by 90, it means the other numbers need to decrease by 30 (3*30 = 90). However this will result in the last two variables being negative.

I could reduce the other variable when a var goes negative, but I don't know how many varibles are in the list.

I cant sum the negative numbers and reduce the positive numbers by that absolute value, as that inturn may make the a number negative again.

I feel like the solution will be some recursion, but I cant think of a nice clean way to do it.

Any thoughts?

##### Share on other sites

Now that we have some real values perhaps we can discuss from chat. Note according to me pulling down 30 by each three-last variable implies you're breaking the ratio between them but I guess that's lost in translation.

a= 1, b=50, c=25, d=24 sum_abcd=100
a=91, b=50, c=25, d=24 sum_abcd=190; sub_bcd=99


a must stay 91. 100 - a = 9 (I guess that's what Madolite tried to tell you). Divide bcd by 100 and now you got a nice 3D vector if you're familiar with this. Normalize it. It'll be

b=.505050, c=.252525, d=.242424 sum_bcd=~100

This is your new 100%. Multiply those values by 9.

b=4.545454~5, c=2.272725~2, d=...~2

What a coincidence, they add up to 9.

I still haven't understood the deal with negatives. You write they cannot go lower than zero... but can start less than zero?

Try counting from the minimum.

Edited by MaxDZ8

##### Share on other sites
For the list, L=(1, 50, 25, 24), and a selected index, idx=0:

1. Find the sum of the others: sum = 50 + 25 + 24 = 99. You may want to cache these ratios until idx changes.
2. After changing L[idx], find remaining amount: remaining = 100-L[idx]
3. Adjust other values according to their proportion, with rounding: (L[jdx]*remaining)/sum.
4. Add or remove 1 to one of the other sliders to correctly account for rounding corner cases, if the sum doesn’t match 100.

EDIT:
Here’s a quick test in powershell:
foreach ( $z in ((50,25,24), (25,25,25)) ) { foreach ($y in (0,1,50,99,100) ) {
$remaining = 100-$y;
$total =$z | measure -sum | select -expand sum;
$others =$z |% {
[Math]::floor(($_*$remaining + $total/2)/$total);
};
$all = ,$y + $others;$sum = $all | measure -sum | select -expand sum;$adj = 100-$sum;$all[1] += $adj;$sum += $adj; "sliders=$all sum=\$sum"
}
}


sliders= ? 0 ? 51 ? 25 ? 24 sum=100
sliders= ? 1 ? 50 ? 25 ? 24 sum=100
sliders= 50 ? 25 ? 13 ? 12 sum=100
sliders= 99 ? ? 1 ? ? 0 ? ? 0 sum=100
sliders=100 ? ? 0 ? ? 0 ? ? 0 sum=100

sliders= ? 0 ? 34 ? 33 ? 33 sum=100
sliders= ? 1 ? 33 ? 33 ? 33 sum=100
sliders= 50 ? 16 ? 17 ? 17 sum=100
sliders= 99 ? ? 1 ? ? 0 ? ? 0 sum=100
sliders=100 ? ? 0 ? ? 0 ? ? 0 sum=100
Edited by fastcall22

##### Share on other sites

Add or remove 1 to one of the other sliders to correctly account for rounding corner cases, if the sum doesn’t match 100.

This part could get a bit arbitrary. If you had {25,25,25,25} and increased one of them by 1, where do you take the 1 from?

I mean you could pick an arbitrary (even random) solution, but I'm wondering about the use case here. Could you just use floats?

##### Share on other sites
It is arbitrary. However, the more I think about it, I think it should go toward the selected slider. Just a thought, though.

1. 1
2. 2
Rutin
21
3. 3
JoeJ
18
4. 4
5. 5

• 14
• 39
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631719
• Total Posts
3001881
×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!