Saturday 29 September 2012

Interactive Hypotrochoid


I've just written an Interactive Hypotrochoid program to play with here:

http://www.dareinteractive.com/hypo/index.html

Looks like this:



Code:



/**
 Dare 2012 Interactive Hypotrochoids, 
 move mouse around to experiment with the
 parameters of this equation, press mouse to re-set.
 very much derived from Archim v2.1
 code here:
 http://www.archimy.com/examples/2d-ellipse.html
 **/

//z must be zero
float z = 0.0;

//t-parameter
float tmin = 0.0;
float tmax = 3.0*PI;//6.0*PI;
float tgrid =120.0;//400.0;

//Constant
float a;// = 5.0;
float b;// = 3.0;
float d;// = 5.0;
float k, m;

void setup() {
  size(600, 600);
  background(0);
}

void draw() {

  translate(width/2, height/2);
  tmin++;
  tmax++;

  a = mouseX%width/3;
  d = mouseX%height/3;
  b = d/6;
  k = a - b;
  m = (a - b) / b;

  //Calculations
  float x = k * cos(tmin) + d * cos(m * tmin);
  float y = k * sin(tmax) - d * sin(m * tmax);
  noStroke();
  fill(120+mouseY%255, mouseY%255, 120+mouseX%255, 220);
  ellipse(x, y, 5, 5);
}

void mousePressed() {
  setup();
}



No comments:

Post a Comment