How to keep the sum of an Array with three or more values equal

edited January 2014 in How To...

I have a bit of a math problem and I can't seem to find an easy solution.

I want to make a function that easily changes the other values in the array if one value of that array changes. (Mathematically something like this (X+Y+Z = 1) And I can think of two versions of this problem/function but I can't find a neat solution for any of them.

One solution is that if I change one value, all the other values have to change too so it doesn't exceed or go below 1 (for example I have (0.3, 0.3, 0.4) and I change the first value to 0.2 the difference gets split over the other two values resulting in (0.2, 0.35, 0.45).

Another solution could be that I specify which value is linked to another value (so only two values are linked, or more if I have more than three values in the array) (for example if I have (0.3, 0.3, 0.4) and I say that the first two values are "linked" , when I then change the first value to 0.2 I get (0.2, 0.4, 0.4).

This looks like a easy problem but I can't find a simple solution. I want to be able to change the size of the array later on without changing the programming.

Does anyone have an idea? Or a mathematical formula that solves this easily? What would be very neat is if I could specify the relation between different values in the array so if one changes I can give a percentage of how much the other values are linked and need to change (for example 0% is no link so no change, 100% is a 1on1 link so if one value changes the other changes equally)

This must be something simple but I can't seem to wrap my head around it. Thanks.

Tagged:

Answers

  • edited January 2014 Answer ✓

    My idea is to create a class to tightly control the add() & sub() processes! Here's my draft: :>

    /** 
     * XYZequals1 (v1.4)
     * by GoToLoop (2014/Jan)
     *
     * forum.processing.org/two/discussion/2280/
     * how-to-keep-the-sum-of-an-array-with-three-or-more-values-equal
     */
    
    class XYZ {
      static final int MAX = 1;
      //static final int MIN = -MAX;
      static final int MIN = 0;
    
      final float[] xyz = {
        .3, .3, .4
      };
    
      XYZ(float x, float y, float z) {
        x = constrain(x, MIN, MAX);
        y = constrain(y, MIN, MAX);
        z = constrain(z, MIN, MAX);
    
        if (x + y + z != 1)  return;
    
        xyz[0] = x;
        xyz[1] = y;
        xyz[2] = z;
      }
    
      void add1Tenth(int idx) {
        if (idx < 0 || idx > 2 || xyz[idx] + .1 > MAX)  return;
        for (int i = 0; i != 3; xyz[i] += i++ == idx? .1 : -.05);
      }
    
      void sub1Tenth(int idx) {
        if (idx < 0 || idx > 2 || xyz[idx] - .1 < MIN)  return;
        for (int i = 0; i != 3; xyz[i] += i++ == idx? -.1 : .05);
      }
    
      float sum() {
        return xyz[0] + xyz[1] + xyz[2];
      }
    
      String toString() {
        return "x=" + xyz[0] + " + y=" + xyz[1]
          + " + z=" + xyz[2] + " = " + sum();
      }
    }
    
  • Thanks, I definitely like the idea for an add and subtracts method, I will work on it a bit more with your code as a base to fit it in my program.

Sign In or Register to comment.