// Inspired by Greenberg, Appendix B - 1 int angle = 0; // current angle float cx = 75, cy = 75, cr = 50; // main circle float br = 10, bo = 40; // balls float go = 40; float gs = 2; // graph float tv = 5; float to = 15; void setup() { // Have to hard-code the size to make the applet happy size(400,400); background(255); smooth(); textFont(loadFont("Helvetica-14.vlw")); textAlign(CENTER); } void draw() { background(255); // text fill(128); text("0",cx+cr-to,cy+tv); text("180",cx-cr+1.5*to,cy+tv); text("90",cx,cy+cr+tv-to); text("270",cx,cy-cr+tv+to); text("+1",cx+cr,cy+cr+bo+tv+go/2); text("-1",cx-cr,cy+cr+bo+tv+go/2); text("+1",cx+cr+bo+go/2,cy+cr+tv); text("-1",cx+cr+bo+go/2,cy-cr+tv); text("0",cx+cr+to,cy+cr+bo+go+tv); text("180",cx+cr+to,cy+cr+bo+go+tv+180/gs); text("360",cx+cr+to,cy+cr+bo+go+tv+360/gs); text("0",cx+cr+bo+go,cy+cr+tv+to); text("180",cx+cr+bo+go+180/gs,cy+cr+tv+to); text("360",cx+cr+bo+go+360/gs,cy+cr+tv+to); fill(0); text("cos",cx+cr+4*to,cy+cr+bo+go+tv+180/gs); text("sin",cx+cr+bo+go+180/gs,cy+cr+tv+4*to); // grdis stroke(200); strokeWeight(1); //line(cx-cr,cy,cx+cr,cy); line(cx-cr,cy+cr+bo,cx+cr,cy+cr+bo); line(cx-cr,cy+cr+bo+go,cx+cr,cy+cr+bo+go); line(cx-cr,cy+cr+bo+go+360/gs,cx+cr,cy+cr+bo+go+360/gs); line(cx-cr,cy+cr+bo+go+180/gs,cx+cr,cy+cr+bo+go+180/gs); line(cx-cr,cy+cr+bo+go,cx-cr,cy+cr+bo+go+360/gs); line(cx+cr,cy+cr+bo+go,cx+cr,cy+cr+bo+go+360/gs); line(cx+cr+bo,cy-cr,cx+cr+bo,cy+cr); line(cx+cr+bo+go,cy-cr,cx+cr+bo+go,cy+cr); line(cx+cr+bo+go+360/gs,cy-cr,cx+cr+bo+go+360/gs,cy+cr); line(cx+cr+bo+go+180/gs,cy-cr,cx+cr+bo+go+180/gs,cy+cr); line(cx+cr+bo+go,cy-cr,cx+cr+bo+go+360/gs,cy-cr); line(cx+cr+bo+go,cy+cr,cx+cr+bo+go+360/gs,cy+cr); noFill(); stroke(0); strokeWeight(2); // main circle ellipse(cx,cy,2*cr,2*cr); // plot the sin and cos graph beginShape(); for(int i = 0; i < 360; i += gs) { vertex(cx+cr+bo+go+i/gs,cy+cr*sin(i*PI/180)); } endShape(); beginShape(); for(int i = 0; i < 360; i += gs) { vertex(cx+cr*cos(i*PI/180),cy+cr+bo+go+i/gs); } endShape(); fill(128); // ball moving over the circle ellipse(cx+cr*cos(angle*PI/180),cy+cr*sin(angle*PI/180),2*br,2*br); // vertical and horizontal balls ellipse(cx+bo+cr,cy+cr*sin(angle*PI/180),2*br,2*br); ellipse(cx+cr*cos(angle*PI/180),cy+cr+bo,2*br,2*br); // plot balls moving on the graph ellipse(cx+bo+go+cr+angle/gs,cy+cr*sin(angle*PI/180),2*br,2*br); ellipse(cx+cr*cos(angle*PI/180),cy+cr+bo+go+angle/gs,2*br,2*br); // animate angle = (angle+1)%360; } // START NO NOTES // code used to capture screenshots void keyReleased() { if(key == '`' && !online) save("sketch.png"); }