float radius, txtSz, angle = PI / 400;
boolean showText = true;
void setup() {
size(2000, 2000);
strokeWeight(width / 200);
smooth();
// textFont(createFont("Inconsolata", width / 20));
textAlign(CENTER, CENTER);
}
void mousePressed() {
showText = !showText;
}
void draw() {
//Scale font and radius by mouse x position
radius = map(mouseX,0,width,width / 2 * 1 / 4,width / 2 * 3 / 4);
txtSz = map(mouseX,0,width,width/40,width/20);
background(44, 62, 80); //Draw background and headers
textSize(width/30);
fill(255);
text("Click to show/hide text",width/2,height/20);
textSize(width/50);
text("Numbers are multiples of radius",width/2,2*height/20);
translate(width / 2, height / 2);
stroke(0);
noFill();
ellipse(0, 0, radius * 2, radius * 2); //draw the big circle
stroke(width/40);
arc(0, 0, radius / 5, radius / 5, 0, angle % (2 * PI)); //draw the arc of angle
//radius, sin, cos, sec, and csc are computed relative to center of circle
float x = cos(angle) * radius, y = sin(angle) * radius; //x,y is the point at end of radius
labeledLine(0, 0, x, y, color(100), "radius", 0);
labeledLine(0, 0, radius / cos(angle), 0, color(255, 0, 255), "sec = " + nfs(1 / cos(angle), 1, 2), 0);
labeledLine(0, y, x, y, color(255, 0, 0), "cos = " + nfs(cos(angle), 1, 2), 0);
labeledLine(x, 0, x, y, color(0, 100, 0), "sin = " + nfs(sin(angle), 1, 2), 0);
labeledLine(0, 0, 0, radius / sin(angle), color(100, 100, 0), "csc = " + nfs(1 / sin(angle), 1, 2), 0);
pushMatrix(); //tan and cot are computed relative to the point at end of radius
rotate(angle);
translate(radius, 0); //move away from the center of the circle by radius with angle
pushMatrix();
rotate(PI / 2); //rotate additional 90 degrees to draw the tangent lines
labeledLine(0, 0, -tan(angle) * radius, 0, color(0, 0, 255), "tan = " + nfs(tan(angle), 1, 2), angle + PI);
labeledLine(0, 0, radius / tan(angle), 0, color(0, 150, 150), "cot = " + nfs(1 / tan(angle), 1, 2), angle + PI);
popMatrix();
popMatrix();
angle += PI * 2 / 60 / 10; //Increment angle
}
//Draw a line from point x1,y1 to x2,y2 with a text label (prevAngle of prior pushMatrices to prevent upside down text)
void labeledLine(float x1, float y1, float x2, float y2, color c, String label, float prevAngle) {
stroke(c, showText ? 60 : 255);
line(x1, y1, x2, y2); //draw the line the text sits on
if(!showText) return;
pushMatrix(); //Draw text at midpoint constrained to not go off screen:
translate((x1 + constrain(x2, -width / 2, width / 2)) / 2, (y1 + constrain(y2, -height / 2, height / 2)) / 2);
float textAngle = atan2(y2 - y1, x2 - x1);
if (degrees((textAngle + prevAngle + 2 * PI)) % 360 >= 180) //keep text from turning upside down
textAngle += PI;
rotate(textAngle); //angle of text
fill(c);
textSize(txtSz);
text(label, 0, -textDescent() / 2);
popMatrix();
}