Need help simplifying the graphing of consecutive lines

edited October 2013 in Questions about Code

Hello. This is my first post here. I am graphing consecutive lines with Processing 2.0.2. I am building the lines using a 3D-gyroscope's coordinates (x,y) and encoder's counts as distances (d) via serial with Arduino UNO. i.e:

line 1:  (0, 0) ----d1----(x1,y1)
line 2:  x1,y1 ----d2---- x2,y2
line 3:  x2,y2 ----d3---- x3,y3
line 4:  x3,y3 ----d4---- x4,y4
etc...

So far my application works OK but as the amount of lines increase, my code is getting bigger and bigger. I need to draw about one hundred consecutive lines, thus, I need help simplifying the code (may be using 'for' or 'while' iterations). Here my draw() function for the first four lines:

 void draw() {
  background(255);
  translate(width/2, height/2);;

  // First concatenated line (not yet fixed)
  if(fixed_lines==0){
  line(0,0,x,y);
  }
  // concatenated lines 1 and 2 (line 1 fixed)
  if(fixed_lines==1){
  line(0,0,x1,y1);
  line(x1,y1,x1+x,y1+y);
  }
  // concatenated lines 1, 2 and 3 (lines 1 and 2 fixed)
  if(fixed_lines==2){
  line(0,0,x1,y1);
  line(x1,y1,x2,y2);
  line(x2,y2,x2+x,y2+x);
  }
  // concatenated lines 1, 2, 3 and 4 (lines 1, 2 and 3 fixed)
  if(fixed_lines==3){
  line(0,0,x1,y1);
  line(x1,y1,x2,y2);
  line(x2,y2,x3,y3);
  line(x3,y3,x3+x,y3+y);
  }
}

Notice that x and y are proportional to the distance d and they become xn and yn after the distance and the second point are fixed. I highly appreciate any help.Thank you.

Answers

  • edited October 2013

    1st off, when we have a list of related variables, we should use an array to hold them all at minimum!
    So, instead of x, x1, x2, x3, x4, etc., we use x[0], x[1], x[2], x[3], x[4], etc.
    This is so to make loops iterating as index for them!

  • Answer ✓

    Some useless craziness I did w/ your code. :-&

    final static int MAX_LINES = 20, VARIATION = 5;
    int x = VARIATION, y = -VARIATION, lines;
    
    void setup() {
      size(500, 500);
      frameRate(1);
      stroke(0);
      strokeWeight(3);
    }
    
    void draw() {
      background(-1);
      translate(width>>1, height>>1);
    
      frame.setTitle("Line: #" + lines);
    
      for (int xx = 0, yy = 0, num = lines; num-- != 0;)
        line(xx, yy, xx += x, yy += y);
    
      if (++lines == MAX_LINES)   lines = 0;
    
      x += random(-VARIATION, VARIATION);
      y += random(-VARIATION, VARIATION);
    }
    
  • Hello GoToLoop, Thank you for your comments. I am using a 2D array in my code.

    float [][] A = new float[100][2];
    

    I just wanted to concentrate the attention in the 'loop' solution. I believe your modification of my code sheds lights to help me to resolve my need. Indeed, my goal is to do it for 3D lines using a 3D array like A[100][3]. Anyway, here my 2D original code.

    void draw() {
      background(255);
      translate(width/2, height/2);;
    
      // First concatenated line (not yet fixed)
      if(fixed_lines==0){
      line(0,0,x,y);
      }
      // concatenated lines 1 and 2 (line 1 fixed)
      if(fixed_lines==1){
      line(0,0,A[0][0],A[0][1]);
      line(A[0][0],A[0][1],A[0][0]+x,A[0][1]+y);
      }
      // concatenated lines 1, 2 and 3 (lines 1 and 2 fixed)
      if(fixed_lines==2){
      line(0,0,A[0][0],A[0][1]);
      line(A[0][0],A[0][1],A[0][0]+A[1][0],A[0][1]+A[1][1]);
      line(A[0][0]+A[1][0],A[0][1]+A[1][1],A[0][0]+A[1][0]+x,A[0][1]+A[1][1]+x);
      }
      // concatenated lines 1, 2, 3 and 4 (lines 1, 2 and 3 fixed)
      if(fixed_lines==3){
      line(0,0,A[0][0],A[0][1]);
      line(A[0][0],A[0][1],A[0][0]+A[1][0],A[0][1]+A[1][1]);
      line(A[0][0]+A[1][0],A[0][1]+A[1][1],A[0][0]+A[1][0]+A[2][0],A[0][1]+A[1][1]+A[2][1]);
      line(A[0][0]+A[1][0]+A[2][0],A[0][1]+A[1][1]+A[2][1],A[0][0]+A[1][0]+A[2][0]+x,A[0][1]+A[1][1]+A[2][1]+y);
      }
    }
    

    As you can see, the code becomes unmanageable very soon. Thank you again. Regards!

Sign In or Register to comment.