Moody Blue Gears

It’s blue. It’s gears. And it’s moody.

I recently needed a new desktop background for my laptop and wanted something industrial and blue. This is what happened. Of course, the desktop image I got from it is static, but it looks much cooler animated. Click it if you get tired of the gears you’re seeing and want different ones to be generated. The Processing code for the animation is at the bottom if you want to play with it yourself. The code was inspired by this gears example by Adrian Fernandez.

/*
Moody Blue Gears Animation
Donya Quick

Click on the screen to re-generate a bunch of randomized spinning gears.

Using a similar gear teeth-drawing strategy to the one from Adrian Fernandez described here:
https://forum.processing.org/two/discussion/6576/simple-gears

Copy and past into Processing to run it. If the indentation is gone, just use
Edit > Auto Format to fix it.
*/
 

Gear[] gears;
 
void setup()
{
  size(600,500);
  frameRate(30);
  buildGears();
}

void buildGears() {
    int numGears = round(random(50,100));
    gears = new Gear[numGears];
    for (int i=0; i<gears.length; i++) {
    float teethHeight = random(width/50,width/20); // gear teeth size
    float x = random(0,width); // center coord
    float y = random(0,height); // center coord
    int r = round(random(width/10,width/5)); // radius
    float b = random(20,200); // how blue?
    float o = random(50,200); // how opaque?
    color c = color(0,0,b,o); // gear color
    float rotSpeed = random(0.005,0.02); // rotation speed (radians)
    if (random(0.0,1.0)>0.5) { // rotate left or right?
      rotSpeed *= -1;
    }
    float rotAngle = random(0,0.4); // rotation angle (radians)
    float sw = random(r/10, r/30); // gear thickness
    int numberOfTeeth = round(random(10,40));
    gears[i] = new Gear(r, x, y, teethHeight, rotSpeed, rotAngle, c, sw, numberOfTeeth);
  }
}

void drawGears() {
  background(0);
  for (int i=0; i<gears.length; i++) {
    gears[i].display();
    gears[i].update();
  }
}

void mousePressed() {
  buildGears();
}
 
void draw()
{
  drawGears();
}

class Gear {
  int r;
  float xPos;
  float yPos;
  float teethHeight;
  float rotSpeed;
  float rotationAngle;
  color c;
  float sw;
  int numberOfTeeth=40;
 
  Gear(int r, float xPos, float yPos, float teethHeight, float rotSpeed, float rotationAngle, color c, float sw, int numberOfTeeth) {
    this.r = r;
    this.xPos = xPos;
    this.yPos = yPos;
    this.teethHeight = teethHeight;
    this.rotSpeed = rotSpeed;
    this.rotationAngle = rotationAngle;
    this.c = c;
    this.sw = sw;
    this.numberOfTeeth = numberOfTeeth;
  }
 
  void update() {
    rotationAngle = rotationAngle + rotSpeed;
  }

  void display()
  {
    float teethAngle=TWO_PI/numberOfTeeth;
    float teethWidth=sin(teethAngle/2)*r;
    float lineY=cos(teethAngle/2)*r+teethHeight;
    pushMatrix();
    translate(xPos, yPos);
    rotate(rotationAngle);
    fill(c);
    noStroke();
    for (int i=0; i<numberOfTeeth; i++)
    {  
      rotate(teethAngle);
      beginShape();
      vertex(-4*teethWidth/4, -lineY+teethHeight);
      vertex(-teethWidth/2, -lineY);
      vertex(teethWidth/4, -lineY);
      vertex(3*teethWidth/4, -lineY+teethHeight);
      endShape(CLOSE);
    }
    noFill();
    ellipseMode(CENTER);
    stroke(c);
    strokeWeight(sw);
    ellipse(0, 0, r*2, r*2);
    popMatrix();
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *