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 & HelpPrograms › Drawing Lines - No Grapical Output
Page Index Toggle Pages: 1
Drawing Lines - No Grapical Output (Read 454 times)
Drawing Lines - No Grapical Output
Jul 20th, 2008, 10:39am
 
Now, being honest, I'm totally new to Processing and I started yesterday. My first programs worked great, but now, I can't even draw lines. (Creating Classes is something like the holy grail to me ^^)

Could someone please just have a quick look at my code and tell me whats wrong?

Code:

void setup() {
size(200,200);
background(0);
smooth();
//noStroke();
frameRate(30);
}

void draw() {
for(int i=0;i<num;i=i+1){
if(vh[i]==1) {
posx[i]=posx[i]+speed[i];
if(posx[i]>width){posx[i]=0;}
//stroke(col[i]);
//fill(col[i]);
stroke(#ffffff);
fill(#ffffff);
line(posx[i],0,posx[i],height);
}

}
}

int num=100;
int[] vh =new int[num];
float[] posx=new float[num];
float[] posy=new float[num];
int[] col =new int[num];
int[] speed =new int[num];
//int[num]
for(int i=0;i<num;i=i+1){
vh[i]=int(random(2));
speed[i]=random(2);
col[i]=color(random(255),random(255),random(255));
posx[i]=random(width);
posy[i]=random(height);
}
Re: Drawing Lines - No Grapical Output
Reply #1 - Jul 20th, 2008, 11:08am
 
your mixing basic with continious mode. init your array in setup not outside of functions:

Code:

void setup() {
size(200,200);
background(0);
smooth();
//noStroke();
frameRate(30);

//int[num]
for(int i=0;i<num;i=i+1){
vh[i]=int(random(2));
speed[i]=int(random(2));
col[i]=color(random(255),random(255),random(255));
posx[i]=random(width);
posy[i]=random(height);
}
}

void draw() {
for(int i=0;i<num;i=i+1){
if(vh[i]==1) {
posx[i]=posx[i]+speed[i];
if(posx[i]>width){
posx[i]=0;
}
//stroke(col[i]);
//fill(col[i]);
stroke(255); // 255 is #ffffff
fill(255);
line(posx[i],0,posx[i],height);
}

}
}

// to be super clean you should only declare/define these here and initialize
// (put values in) inside setup.
int num=100;
int[] vh =new int[num];
float[] posx=new float[num];
float[] posy=new float[num];
int[] col =new int[num];
int[] speed =new int[num];


F
Re: Drawing Lines - No Grapical Output
Reply #2 - Jul 20th, 2008, 11:24am
 
Thanks alot ^^ Now its finally working, but theres another problem... some lines wont move, maybe you can help me on this too? Would be very nice of you ^^

Current Code:
Code:


void setup() {
size(200,200);
background(0);
smooth();
//noStroke();
frameRate(30);

//int[num]
for(int i=0;i<num;i=i+1){
vh[i]=int(random(2));
speed[i]=int(random(2));
col[i]=color(random(255),random(255),random(255));
posx[i]=random(width);
posy[i]=random(height);
}
}

void draw() {
background(0);
for(int i=0;i<num;i=i+1){
if(vh[i]==1) {
posx[i]=posx[i]+speed[i];
if(posx[i]>width){
posx[i]=0;
}
stroke(col[i]);
line(posx[i],0,posx[i],height);
}
if(vh[i]==0) {
posy[i]=posy[i]+speed[i];
if(posy[i]>height){
posy[i]=0;
}
stroke(col[i]);
line(0,posy[i],width,posy[i]);
}

}
}

// to be super clean you should only declare/define these here and initialize
// (put values in) inside setup.
int num=100;
int[] vh =new int[num];
float[] posx=new float[num];
float[] posy=new float[num];
int[] col =new int[num];
int[] speed =new int[num];
Re: Drawing Lines - No Grapical Output
Reply #3 - Jul 20th, 2008, 12:00pm
 
Short answer: that's because speed is random(2) ie. a number between 0 (inclusive) and 2 (exclusive). Lines with speed 0 won't move...

Long answer: the fix, and some little improvements, to show some common idioms in Java (and C/C++/similar languages):

i=i+1 => i++
num => NUM (convention to spot constants) See Static Final for a good explanation of constants
increment plus test above a value, then return at 0 => use of % (modulo)
using a boolean instead of an integer with only two values used only in tests

Resulting code:
Code:

void setup() {
size(200,200);
background(0);
smooth();
//noStroke();
frameRate(30);

//int[NUM]
for(int i=0;i<NUM;i++){
vh[i]=random(2) > 1;
speed[i]=int(random(1,3));
col[i]=color(random(256),random(256),random(256));
posx[i]=random(width);
posy[i]=random(height);
}
}

void draw() {
background(0);
for(int i=0;i<NUM;i++){
stroke(col[i]);
if(vh[i]) {
posx[i]=(posx[i]+speed[i]) % width;
line(posx[i],0,posx[i],height);
} else {
posy[i]=(posy[i]+speed[i]) % height;
line(0,posy[i],width,posy[i]);
}
}
}

// static final just indicate it is a constant
static final int NUM=100;
boolean[] vh =new boolean[NUM];
float[] posx=new float[NUM];
float[] posy=new float[NUM];
int[] col =new int[NUM];
int[] speed =new int[NUM];

Also note I incremented the values in random, since the upper bound is never reached.
Page Index Toggle Pages: 1