We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
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!
Some useless craziness I did w/ your code. :-&
Hello GoToLoop, Thank you for your comments. I am using a 2D array in my code.
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.
As you can see, the code becomes unmanageable very soon. Thank you again. Regards!