// COMP1405/1005 - Day 09 // dymanic spirograph // variables float x, y; // current x,y positions float xp, yp; // previous x,y positions float a, b, k; // paramaters for curve int count = 0; float t = 0; // initialize void setup() { size(600, 400); background(2, 100, 2); fill(0); stroke(252, 252, 0); strokeWeight(2.5); smooth(); b = 100; // this needs to be adjusted to fit the curve // nicely to the screen k = 1.6; // this defines the curve a = k*b; // initial (previous point) xp = (a-b)*cos(t) + b*cos(t*(k-1)); yp = (a-b)*sin(t) - b*sin(t*(k-1)); } // main body void draw() { // t is a scaled version of our count // trig functins (cos, sin, etc) expect the input // values to correspond to radians (not degrees). // I will compute successive points on the curve // by increments of 1/(4*PI). // Recall that 180 degrees (1/2 circle) is PI radians. count += 1; t = count/PI/4; // compute the new point x = (a-b)*cos(t) + b*cos(t*(k-1)); y = (a-b)*sin(t) - b*sin(t*(k-1)); // instead of drawing points, I'll draw a line from the // previous point to the current point. This only works // the points (x,y) and (xp,yp) are close enough together // (which is why I scaled the count) // notice that I shift everything over so that the curve is // centred in the middle of the screen. I also want the curve // to be oriented correctly, which is why I am subtracting the y(yp) // values from the middle. line(xp+width/2, height/2-yp, x+width/2, height/2-y); // let the current point be the new previous point and repeat the loop... xp = x; yp = y; }