| Up: Experiments in Processing | [Related] «^» «T» |
Monday, September 1, 2003
Red Rotator
By Paul Ford
Left, right, up, down, click.
/* Draws a pile of candycane-colored squares that
spin above one another. Mouse moves the squares
off the center; clicking turns the squares into
circles. By Paul Ford, ford@ftrain.com, who is
learning. GPL copyright 2003. */
float b;
int press=0;
int squareno=12;
void setup() {
size(200, 200);
background(0);
rectMode(CENTER_DIAMETER);
ellipseMode(CENTER_DIAMETER);
}
void loop() {
// Normalize mouse coordinates to be
// the absolute value of the mouse's
// distance from the center of the grid
float normalX=abs(width/2-mouseX);
float normalY=abs(height/2-mouseY);
// And make 0,0 the same location
translate(width/2,height/2,0);
//Draw squareno's worth of squares
for (int i=0;i<=width;i+=width/squareno){
//Set up an incrementer called b that
//is used to se the angle of rotation.
b+=0.00002;
if(b >= PI*4) {
b=0;
}
//Normalize the fill color.
int fill=int(255 * i/width);
smooth();
noStroke();
push();
//Rotate each square by its place
//in the stack of squares (i)
//and by the status of the rotator (b).
rotateZ(i*b*2);
//Move things around according
//to the mouse, and set the increasing Z axis
//of each square.
translate(normalX/4,normalY/4,i/10);
// Draw a rectangle if the mouse is up, an
// ellipse if it's down.
if (press==0) {
fill(fill);
rect(0,0,width+width/20-i,width+width/20-i);
fill(fill-20,0,0);
rect(0,0,width-i,width-i);
}
else {
fill(fill);
ellipse(0,0,width+width/20-i,width+width/20-i);
fill(fill-20,0,0);
ellipse(0,0,width-i,width-i);
}
pop();
}
}
void mousePressed() {
press=1;
}
void mouseReleased() {
press=0;
}
Built with Processing.