• fullscreen
  • Gear.pde
  • GearDemo.pde
  • class Gear {
    
      ArrayList<Vec2D> array = new ArrayList<Vec2D>();
    
      float radius;
      Vec2D center = new Vec2D();
      float teethHeight = 10;
      float teeth_factor = 2;
    
      int numberOfTeeh = 0;
      float teethAngle;
    
      Gear(float radius) {
        this.radius = setRadius(radius);
    
        buildGear();
      }
    
      void buildGear() {
        numberOfTeeh = 0;
        teethAngle = 0;
    
        array = new ArrayList<Vec2D>();
        ArrayList<Vec2D> circle = createArc(center, radius);
    
        Vec2D p1 = circle.get(0);
        Vec2D p0;
        for (int i=1; i<circle.size(); i++) {
          p0 = circle.get(i);
          teeth(p1, p0);
    
          p1=p0;
        }
        array.add(array.get(0));
      }
    
      void teeth(Vec2D p1, Vec2D p0) {
    
    
        if (p1.equalsWithTolerance(p0, 0.01f)) {
          return;
        }
    
        this.numberOfTeeh++;
    
        Vec2D p4;
        Vec2D p3;
        p3 = p0.add(p1).scale(0.5f);
        Line2D l = new Line2D(center, p3);
        p4 = new Ray2D(p3, l.getDirection()).getPointAtDistance(teethHeight);
    
        array.add(p1);
        array.add(p4);
      }
    
      ArrayList<Vec2D> createArc(Vec2D center, float radius) {
        ArrayList<Vec2D> points = new ArrayList<Vec2D>();
        float theta = 0;
        float delta = PApplet.TWO_PI/(radius*(1f/teeth_factor));
        teethAngle = delta;
        for (; theta<PApplet.TWO_PI; theta+=delta) {
          Vec2D p=new Vec2D(radius, theta).toCartesian().addSelf(center);
          points.add(p);
        }
        points.add(new Vec2D(radius, PApplet.TWO_PI).toCartesian().addSelf(center));
    
        return points;
      }
    
    
      float setRadius(float _radius) {
        radius = _radius - _radius%teeth_factor;
        buildGear();
        return radius;
      }
    
      ArrayList<Vec2D> getPoints() {
        return array;
      }
    
      float getRadius() {
        return radius;
      }
    
      Vec2D getCenter() {
        return center;
      }
    
      void setCenter(Vec2D center) {
        this.center = center;
        this.buildGear();
      }
    
      float getTeethHeight() {
        return this.teethHeight;
      }
    
      float getTeethNumber() {
        return numberOfTeeh;
      }
    
      float getTeethAngle() {
        return teethAngle;
      }
    }
    
    
    import toxi.math.conversion.*;
    import toxi.geom.*;
    import toxi.math.*;
    import toxi.geom.mesh2d.*;
    import toxi.util.datatypes.*;
    import toxi.util.events.*;
    import toxi.geom.mesh.subdiv.*;
    import toxi.geom.mesh.*;
    import toxi.math.waves.*;
    import toxi.util.*;
    import toxi.math.noise.*;
    
    
    Gear big;
    Gear small;
    Vec2D smallCenter = new Vec2D();
    float t = 0;
    int example = 0;
    
    void setup() {
      size(400, 400);
      big = new Gear(75);
      small = new Gear(50);
      smooth();
    }
    
    void draw() {
      t += 0.01f;
      background(255);
      translate(width/2, height/2);
    
      switch (example) {
      case 0:
        example0();
        break;
      case 1:
        example1();
        break;
      case 2:
        example2();
        break;
    
      default:
        break;
      }
    }
    
    
    private float getGearRatio(Gear a, Gear b) {
      float r = ((float)(a.getTeethNumber())) / ((float)(b.getTeethNumber())) ;
      //System.out.println(r);
      return r;
    }
    
    private void example2() {
      float r =  getGearRatio(big, small) +1;
      float h = PI + PI/small.getTeethNumber();
    
      pushMatrix();
      rotate( t*r );
      drawGear(big);
      popMatrix();
    
      smallCenter.y = 0;
      smallCenter.x = ( big.getRadius() + small.getRadius() +big.getTeethHeight());
      translate(smallCenter.x, smallCenter.y);
    
      rotate( -t*r *(getGearRatio(big, small)) +h  );
      drawGear(small);
    }
    
    private void example1() {
      drawGear(big);
      smallCenter.x = cos(t) * ( big.getRadius() - small.getRadius() );
      smallCenter.y = sin(t) * ( big.getRadius() - small.getRadius() );
      translate(smallCenter.x, smallCenter.y);
      float r = getGearRatio(big, small) -1;
      rotate( - t*r );
    
      drawGear(small);
    }
    
    private void example0() {
    
      float h = PI + PI/small.getTeethNumber();
    
      drawGear(big);
    
      smallCenter.x = cos(t) * ( big.getRadius() + small.getRadius() +big.getTeethHeight());
      smallCenter.y = sin(t) * ( big.getRadius() + small.getRadius() +big.getTeethHeight());
    
      translate(smallCenter.x, smallCenter.y);
      float r =  getGearRatio(big, small) +1;
      rotate( t*r + h);
      drawGear(small);
    }
    
    void drawGear(Gear g) {
      int i = 0;
      Vec2D p1 = g.getPoints().get(0);
      for (Vec2D p0 : g.getPoints()) {
        stroke(0);
    
        strokeWeight(1);
        line(p0.x, p0.y, p1.x, p1.y);
        p1=p0;
        i++;
      }
    }
    
    void keyPressed() {
      example = (example+1)%3;
    }
    
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    nkint

    GearDemo

    Add to Faves Me Likey@! 2
    You must login/register to add this sketch to your favorites.

    press a key for next movement example.
    thanks to sam @ http://sam.zoy.org/

    You need to login/register to comment.