void setup(){
size(400, 400);
noFill();
}
void draw(){
//vertices
float[] a = {mouseX, mouseY};
float[] b = {width/3, height/2};
float[] c = {2*width/3, height/2};
background(200);
//the triangle
triangle(a[0], a[1], b[0], b[1], c[0], c[1]);
//the 3 angle bisector lines
drawBisector(b[0], b[1], a[0], a[1], true);
drawBisector(c[0], c[1], a[0], a[1], false);
float A1 = a[1]-b[1];
float A2 = a[1]-c[1];
float B1 = a[0]-b[0];
B1 += dist(0, 0, A1, B1);
float B2 = a[0]-c[0];
B2 -= dist(0, 0, A2, B2);
float C1 = B1*b[1] - A1*b[0];
float C2 = B2*c[1] - A2*c[0];
float CY = (C1 - C2*(A1/A2))/(B1 - B2*(A1/A2));
float CX = (B1*CY - C1)/A1;
float CD = abs(height/2 - CY);
ellipse(CX, CY, CD*2, CD*2);
}
void drawBisector(float C1, float C2, float P1, float P2, boolean D){
//assuming the second line is horizontal (construction advantage)
//the equation of the line is generated by the equality of the distance
//between a point and 2 lines (bisector aspect)
// A, B, and C represent the standard form of a line eq. (Ax + By + C = 0)
float A = P2 - C2;
float B = P1 - C1;
float C = B*C2 - A*C1; //plugging one of the points to get C
//the distance formula from a point on the line is |Ax - By + C|/hypot(A, B)
//stroke(0,100,0);
//drawLine(A, B, C);
if (D){
B += dist(0, 0, A, B);
}else{
B -= dist(0, 0, A, B);
}
C = B*C2 - A*C1;
stroke(100, 0, 0);
drawLine(A, B, C);
stroke(0);
}
void drawLine(float A, float B, float C){ //from the standard form (Ax - By + C = 0
if (A*B < 0){ //if it's a negative slope (for interception purposes)
line(0, C/B, -C/A, 0);
}
else{ //if it's a positive slope
line(width, (C+A*width)/B, -C/A, 0);
}
}