// COMP 1405/1005 - Day 10 // Function Example with time delay // variables // Note: these variables should really not be declared here. // They really only have meaning "inside" the drawStar() // function, so we should declare and use them inside // that function only. (See spiro2.pde example) // We should only declare variables here that need to be // accessed from anywhere in the program (from any function) float x, y, xp, yp; // current and previous point on curve float a, b, k; // paramaters for curve // initialize void setup() { size(600, 400); background(2, 100, 2); fill(0); textSize(35); smooth(); stroke(252, 252, 0); strokeWeight(1.5); b = 10; // this needs to be adjusted to fit // the curve nicely to the screen k = 4.5; // this defines the curve a = k*b; } // main body void draw() { int x0 = (int) random(25, width-25); int y0 = (int) random(25, height-25); drawStar(x0, y0); pause(0.75); } // pause : float -> nothing // Purpose : to cause Processing to do nothing // (or pause) for a given time // Example : pause(2.0) should cause a pausing of 2 seconds // pause(0.5) should cause half a second delay void pause(float time){ // get the time when the function starts int start = millis(); // do nothing until the current time is // greater than the start time plus // the delay time (input paramater time) while( millis() < start + time*1000 ){ // why did I multiply time by 1000? // do nothing in the actual loop.... } } void drawStar(int x0, int y0){ // initial point on curve to plot xp = (a-b)*cos(0) + b*cos(0*(k-1)); yp = (a-b)*sin(0) - b*sin(0*(k-1)); // compute successive points and draw // a line between current and previous point // let current point be previous poiunt after // we draw the line for( float t = 1.0/PI/6; t < 25; t+= 1.0/PI/6){ x = (a-b)*cos(t) + b*cos(t*(k-1)); y = (a-b)*sin(t) - b*sin(t*(k-1)); line(x0+xp, y0-yp, x0+x, y0-y); xp = x; yp = y; } }