We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Question about Arrays
Page Index Toggle Pages: 1
Question about Arrays (Read 644 times)
Question about Arrays
Dec 6th, 2009, 9:54am
 
Hello folks,

I've been working on a sketch using an array to store a mouseX position to determine the radius of a circle (in the final code I will be using the serial port to read input from an arduino instead of mouseX). The problem is that I want to create an unbroken line linking each new point to the previous point in the array - I presume there's a relatively simple fix that I'm missing?

Here's my code:
Code:
float[] xpos = new float[120];
float[] ypos = new float[120];

float r = 0.0;

void setup(){
 size(500, 500);
 smooth();

 for (int i = 0; i<xpos.length; i++){
   xpos[i] = 0;
   ypos[i] = 0;
 }
}

void draw(){
 r = map(millis()*0.05, 0, 100, 0, TWO_PI);
 float rx = sin(r)*mouseX+width/2;
 float ry = cos(r)*mouseX+height/2;

 background(0);

 for (int i = 0; i<xpos.length-1; i++){
   xpos[i] = xpos[i+1];
   ypos[i] = ypos[i+1];
 }

 xpos[xpos.length-1] = rx;
 ypos[ypos.length-1] = ry;

 for (int i = 0; i<xpos.length-1; i++){
   stroke(255);
   noFill();
   line(xpos[i], ypos[i], xpos[i], ypos[i]);
 }
}


Thanks in advance! Smiley
Re: Question about Arrays
Reply #1 - Dec 6th, 2009, 11:59am
 
I think I may have fixed it, added a second array [j] I presume that is an appropriate way to solve the problem.

Code:
float[] xpos = new float[120];
float[] ypos = new float[120];

float r = 0.0;

void setup(){
size(500, 500);
smooth();

for (int i = 0; i<xpos.length; i++){
xpos[i] = 0;
ypos[i] = 0;
}
}

void draw(){
r = map(millis()*0.05, 0, 100, 0, TWO_PI);
float rx = sin(r)*mouseX+width/2;
float ry = cos(r)*mouseX+height/2;

background(0);
stroke(255);
noFill();

for (int i = 0; i<xpos.length-1; i++){
xpos[i] = xpos[i+1];
ypos[i] = ypos[i+1];
}

xpos[xpos.length-1] = rx;
ypos[ypos.length-1] = ry;

beginShape(LINES);
for (int i = 0; i<xpos.length-1; i++){
vertex(xpos[i], ypos[i]);
}
for (int j = 0; j<xpos.length; j++){
vertex(xpos[j], ypos[j]);
}
endShape();
}

Re: Question about Arrays
Reply #2 - Dec 6th, 2009, 12:16pm
 
No need for the second array - needs improving but this should give you the idea:

Code:
float[] xpos = new float[120];
float[] ypos = new float[120];

float r = 0.0;

void setup(){
size(500, 500);
smooth();

for (int i = 0; i<xpos.length; i++){
xpos[i] = 0;
ypos[i] = 0;
}
}

void draw(){
r = map(millis()*0.05, 0, 100, 0, TWO_PI);
float rx = sin(r)*mouseX+width/2;
float ry = cos(r)*mouseX+height/2;

background(0);
stroke(255);
noFill();

for (int i = 0; i<xpos.length-1; i++){
xpos[i] = xpos[i+1];
ypos[i] = ypos[i+1];
}

xpos[xpos.length-1] = rx;
ypos[ypos.length-1] = ry;

for (int i = 1; i<xpos.length; i++){
line(xpos[i-1], ypos[i-1], xpos[i], ypos[i]);
}
}
Re: Question about Arrays
Reply #3 - Dec 7th, 2009, 1:14am
 
I honestly thought I tried that already!! I must have been half asleep or something. Thanks!

//Screws head on properly
Page Index Toggle Pages: 1