// COMP1405/1005 - Day 10 (extra) // dymanic spirograph // draw a "star" on the screen at the current mouse location // whenever you click the mouse button. The type of star // is randomly chosen // variables int count; // let's count how many stars we draw // initialize void setup() { size(600, 400); background(2, 100, 2); fill(0); textSize(35); smooth(); stroke(252, 252, 0); strokeWeight(2.5); count = 0; } // main body void draw() { if( count < 1){ text("Press the mouse to draw a star", width/10, height/2); } } // Draw a star at the current mouse location // when the mouse is pressed. The type of // star is chosen randomly. void mousePressed() { background(0, 100, 0); count += 1; drawStar(mouseX, mouseY, (int) random(3, 11)); } // drawStar: float, float, float -> nothing // Purpose : Inputs three floats and draws a "star" on the screen // centred at the coordinates specified by the // first two inputs. The "type" of star is specified // by the third/last input parameter. // Examples : drawStar(100,100,3); // draws a three-pointed star centred at (100,100) // drawStar(200,100,5); // draws a five-pointed star centred at (200,100) void drawStar(float xo, float yo, float ko) { // All the variables needed to draw the star // can be local to the drawing function itself. float x,y,xp,yp; float b = 10; float a = ko * b; float step = 1.0/PI/4; xp = (a-b)*cos(0) + b*cos(0*(ko-1)); yp = (a-b)*sin(0) - b*sin(0*(ko-1)); for (float t = 0.0; t<20; t+= step) { x = (a-b)*cos(t) + b*cos(t*(ko-1)); y = (a-b)*sin(t) - b*sin(t*(ko-1)); line(xo+xp, yo-yp, xo+x, yo-y); xp = x; yp = y; } fill(255); text("k="+ko, 5, height-5); text("(" + count + " stars drawn)", 150, height-5); }