MENU

spirograph(t)

The classic children's toy is a physical implementation of the Hypotrochoid. Different curves result from the choice of different parameters, in the form of disk sizes. Here we have a digital implementation, which allows us to see what happens if the parameters vary as a function of time. Programmed in the Processing language.

View the code

boolean toggleLoop = false;

 

void setup() {

  noLoop();

  size(800, 800);

  fill(#ffffff);

  strokeWeight(1);

  

}

 

void draw() {

  

  int record=0;

  

  translate(width/2,height/2);

 

  noStroke();

  rect(-width/2, -height/2, width, height);

  stroke(0);

 

  float R=0.3*height;

  float ratio=0.3; //no less than 0.001

  float r=R*ratio;

  float d=r*(0.5+2*(0.5+0.5*sin(TWO_PI*frameCount/175-TWO_PI/4)));

 

  int rolls=0;

  float numerRolls0=round(1000*ratio);

  float denomRolls0=1000;

  float numerRolls1;

  float denomRolls1;

 

  float[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997};

 

  for (int q=0; q<primes.length; q=q+1) {

    numerRolls1=numerRolls0/primes[q];

    denomRolls1=denomRolls0/primes[q];

    while (round(numerRolls1)==0.1*round(10*numerRolls1) && 0.1*round(10*denomRolls1)==round(denomRolls1)) {

      numerRolls0=numerRolls1;

      numerRolls1=numerRolls0/primes[q];

      denomRolls0=denomRolls1;

      denomRolls1=denomRolls0/primes[q];

    }

  }

 

  rolls=int(numerRolls0);

  println("rolls="+rolls);

 

  float X0;

  float X1;

  float Y0;

  float Y1;

 

  float theta=0;

  X0=(R-r)*cos(theta)+d*cos((R-r)*theta/r);

  Y0=(R-r)*sin(theta)-d*sin((R-r)*theta/r);

 

  for (int i=1; i<rolls*360+1; i=i+1) {

    theta=i*TWO_PI/360;

    X1=(R-r)*cos(theta)+d*cos((R-r)*theta/r);

    Y1=(R-r)*sin(theta)-d*sin((R-r)*theta/r);

    line(X0,Y0,X1,Y1);

    X0=X1;

    Y0=Y1;

  }

    

  if ((record==1) && (frameCount<=rolls*360)) {

    saveFrame("gif/f####.gif");

  }

 

  println("framecount="+frameCount);

 

  if (toggleLoop == false) {

      textBox();

  }

 

}

 

void mousePressed() {

  if (mouseButton == LEFT) {

    if (toggleLoop) { noLoop(); toggleLoop = false; textBox();}

    else { loop(); toggleLoop = true; }

  }

}

 

void textBox() {

  noStroke();

  fill(0,0,0,80);

  rect(-0.5*0.7*width,-0.5*0.1*height,0.7*width,0.1*height);

  textAlign(CENTER, CENTER);

  textSize(60);

  text("click to start/stop",0,-0.015*height);

  stroke(0,0,0,255);

  fill(255,255,255,255);

}