Skip to content

Instantly share code, notes, and snippets.

@masaakif
Created January 13, 2009 03:17
Show Gist options
  • Save masaakif/46305 to your computer and use it in GitHub Desktop.
Save masaakif/46305 to your computer and use it in GitHub Desktop.
Processing - Draw Square
int MAX = 500;
RectDrawer[] rd;
void setup() {
rd = new RectDrawer[MAX];
size(800, 800, P3D);
noStroke();
randomSeed(0);
frameRate(30);
for (int i = 0; i < MAX; i++) {
rd[i] = null;
}
}
void draw() {
background(128,128,128);
for (int i = 0; i < MAX; i++)
{
if (rd[i] != null) {
rd[i].drawRect();
if (rd[i].isLiving() == false) {
rd[i] = null;
}
}
}
}
void mouseDragged()
{
for (int i = 0; i < MAX; i++) {
if (rd[i] == null) {
rd[i] = new RectDrawer(mouseX, mouseY, random(10,25), getColor());
break;
}
}
}
color getColor()
{
float fr = random(0,255);
float fg = random(0,255);
float fb = random(0,255);
return color(fr,fg,fb);
}
class RectDrawer {
int life = 255;
float xpos, ypos, rectsize;
color cl;
float rad;
float rotx, roty, rotxd, rotyd;
int type; /* 1 = Rect, 2 = Triangle, 3 = Ellipse */
RectDrawer(float x, float y, float s, color c) {
xpos = x; ypos = y; rectsize = s; cl = c;
rad = random(0, PI*2);
rotx = 0; roty = 0;
rotxd = random(-5,5); rotyd = random(-5,5);
}
public boolean isLiving() {
if (-20 < life) return true;
return false;
}
void drawRect() {
int alp = life > 0 ? life : 0;;
pushMatrix();
translate(xpos - rectsize/2, ypos - rectsize/2);
rotate(rad);
rotateX(radians(rotx)); rotateY(radians(roty));
fill(color(red(cl), green(cl), blue(cl), alp));
rect(0,0, rectsize, rectsize);
popMatrix();
onetick();
}
void onetick()
{
life -= 5;
rotx += rotxd;
roty += rotyd;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment