/** * a gear demo by Guru */ void setup() { size(300,300); smooth(); frameRate(25); } float a = 0; void draw() { background(255); a += 0.05; strokeWeight( 2 ); fill(255,0,0); pushMatrix(); translate( width/2, height/2 ); rotate( a ); gear( 8, 40, 50, PI/32 ); popMatrix(); fill(255,255,0); pushMatrix(); translate( width/2, height/2-90 ); rotate( -a ); gear( 8, 40, 50, PI/32 ); popMatrix(); fill(0,0,255); pushMatrix(); translate( width/2+90, height/2 ); rotate( -a ); gear( 8, 40, 50, PI/32 ); popMatrix(); fill(0,255,0); pushMatrix(); translate( width/2-64, height/2+64 ); rotate( -a ); gear( 8, 40, 50, PI/32 ); popMatrix(); } void gear( int tooth, int ri, int ro, float o ) { beginShape(); for( int i = 0; i < tooth; i++ ) { vertex(cos(2*PI/tooth * i - o ) * ri, sin(2*PI/tooth*i - o) * ri); vertex(cos(2*PI/tooth * i + o ) * ro, sin(2*PI/tooth*i + o) * ro); vertex(cos(2*PI/(2*tooth) * (2*i+1) - o) * ro, sin(2*PI/(2*tooth)*(2*i+1) - o) * ro); vertex(cos(2*PI/(2*tooth) * (i*2+1) + o) * ri, sin(2*PI/(2*tooth)*(2*i+1) + o) * ri); } endShape(CLOSE); }