« Turn any window into a speaker | Main | Processing: Pulsing Circles! »

Processing: Ghosting Circles

I recently wrote a neat little program in processing that allows a user to place circles (by clicking the screen) and move them around by dragging the mouse. Each circle leaves a ghostly trail behind it as it moves. Paste the code into processing to experience it yourself!!!

-------

/* Sean's circle tracking extravaganza! it's the winningest!
* In this program, the user can click anywhere on
* screen and a ball of random color and size will
* appear on the screen! when the user moves the
* mouse around, the balls will move with some
* random correlation to the mouse movement
*
*/

//number of circles
int numCircs = 30;

circle[] circArray = new circle[numCircs];

void setup() {
//initialize all the circles
for(int i = 0; i < numCircs; i++) {
circArray[i] = new circle();
}


size(1024,768);
background(0);
fill(random(255),random(255),random(255));
noStroke();


//you can adjust the frame rate if you want O_O_O_O
//frameRate(100);

}

void draw() {
translate(1024/2, 768/2);
background(0);
for (int i = 0; i circArray[i].update();
}
}

void mousePressed() {
//draw an ellipse with a random size
for (int i = numCircs-1; i>0 ; i--) {
circArray[i]=circArray[i-1];
}
circArray[0] = new circle(mouseX-1024/2, mouseY-768/2, random(10,45), random(-2,2), random(-2,2), int(random(10,80)));
}


class circle {
float[] xPos;
float[] yPos;
float radius;
float factorX;
float factorY;
int numTrail;
int trackme = 0;

float c1 = random(255);
float c2 = random(255);
float c3 = random(255);

circle() {
numTrail = 1;
xPos = new float[numTrail];
yPos = new float[numTrail];
for(int i = 0; i xPos[i] = 0;
yPos[i] = 0;
}
radius = 0;
factorX = 0;
factorY = 0;
}

circle(float x,float y,float r,float fx, float fy, int Trails) {
numTrail = Trails;
xPos = new float[numTrail];
yPos = new float[numTrail];

for(int i = 0; i xPos[i] = x;
yPos[i] = y;
}
radius = r;
factorX = fx;
factorY = fy;
}

void update() {
trackme++;
rotate(PI*trackme*factorX*.5/4000);

float mouseChangeX = mouseX-pmouseX;
float mouseChangeY = mouseY-pmouseY;

// Reads throught the entire array
// and shifts the values to the left
for(int i=1; i xPos[i-1] = xPos[i];
yPos[i-1] = yPos[i];
}
// Add the new values to the end of the array
xPos[numTrail-1] += mouseChangeX*factorX;
yPos[numTrail-1] += mouseChangeY*factorY;

fill(c1,c2,c3, 28);
for(int i=0; i float divisor = (radius/numTrail)*i;
ellipse(xPos[i], yPos[i], divisor, divisor);
}

}

}

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on January 26, 2009 2:34 PM.

The previous post in this blog was Turn any window into a speaker.

The next post in this blog is Processing: Pulsing Circles!.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.31