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();
};