So I'm working on a Civ like game right now and want to have a setting how taxes are split in gold income, science and luxury goods.
private double[] rate = new double[] { 0.4, 0.6, 0.0 };
now I wanted to add a methode to change one of the values to a higher or lower value but maintain the overall sum of 1.0 (100%) but I'm stuck here with how to take or add values evenly from the other two rates
public void setRate (int index, double r) {
if (r < 0.0) {
throw new IllegalArgumentException("Invalid Rate");
}
double diff = r - rate[index];
if (diff > 0) {
// ?
} else if (diff < 0) {
// ?
}
rate[index] = r;
}
EDIT for amitp:
public void setRate (int index, double r) {
if (rate[index] = 1.0) {
for (int i = 0; i < 3; i++) {
if (i != index) {
rate[i] = (rate[index] - r) / 2;
}
}
} else {
for (int i = 0; i < 3; i++) {
if (i != index) {
rate[i] = rate[i] * (1.0 - r) / (1.0 - rate[index]);
}
rate[index] = r;
}
}