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.
IndexDiscussionExhibition › Firefly sketch
Page Index Toggle Pages: 1
Firefly sketch (Read 1244 times)
Firefly sketch
Dec 1st, 2007, 8:05pm
 
As a Processing newcomer, I need some advice on how to improve my first sketch.
- Fireflies.
Code:

// initialize positions
float fax;
float fay;
float fbx;
float fby;
float fcx;
float fcy;


void setup () {
background(0);
size(512,512);
stroke(255,15);
strokeWeight(10);
fill(0,5);
fax = random(200,300);
fay = random(200,300);
fbx = random(150,350);
fby = random(150,350);
fcx = random(150,350);
fcy = random(150,350);
};
void draw () {
stroke(0,5);
rect(0,0,height,width);
//firefly 1
float cx = random(-4,4);
float cy = random(-4,4);
fax = fax + cx;
fay = fay + cy;
float z;
if(sqrt(sq(fax - fbx) + sq(fay - fby)) < sqrt(sq(fax - fcx) + sq(fay - fcy))) {
z = random(sqrt(sq(fax-fbx) + sq(fay-fby)));
}
else {
z = random(sqrt(sq(fax-fcx) + sq(fay-fcy)));
};
if(z < 1) {
stroke(random(180,220),255,0,100);
}
else {
stroke(150,15);
};
point(fax,fay);

//firefly 2
cx = random(-4,4);
cy = random(-4,4);
fbx = fbx + cx;
fby = fby + cy;
if(sqrt(sq(fax - fbx) + sq(fay - fby)) < sqrt(sq(fbx - fcx) + sq(fby - fcy))) {
z = random(sqrt(sq(fax-fbx) + sq(fay-fby)));
}
else {
z = random(sqrt(sq(fbx-fcx) + sq(fby-fcy)));
};
if(z < 1) {
stroke(random(180,220),255,0,100);
}
else {
stroke(150,15);
};
point(fbx,fby);

//firefly 3
cx = random(-4,4);
cy = random(-4,4);
fcx = fcx + cx;
fcy = fcy + cy;
if(sqrt(sq(fcx - fbx) + sq(fcy - fby)) < sqrt(sq(fax - fcx) + sq(fay - fcy))) {
z = random(sqrt(sq(fcx-fbx) + sq(fcy-fby)));
}
else {
z = random(sqrt(sq(fax-fcx) + sq(fay-fcy)));
};
if(z <1) {
stroke(random(180,220),255,0,100);
}
else {
stroke(150,15);
};
point(fcx,fcy);
};

void mousePressed() {
setup();
};
Re: Firefly sketch
Reply #1 - Dec 3rd, 2007, 10:43am
 
at first sight,
i would use opengl to render shapes and i would not compare real distances. you can compare square distances without the square roots and saving a heavy computation there.

with those 2 options i get a pretty reasonable framerate here.. i leave the rest for you. im sure you are able to put alot more fireflies on the screen.
Page Index Toggle Pages: 1