xxxxxxxxxx
217
// cleanup and rearrange for new forum question,
// but still not found the ERROR
var d = 7;
var x1 = 80, y1 = 60;
var x2 = 250,y2 = 30;
// mouse point for circum_circle play
var x3 = 350,y3 = 250; // about if y3 < y1 ERROR in calc
var L12, L23, L31, All, A1, A2, A3, M12x, M12y, M23x, M23y, M31x, M31y;
var CR, CRx, CRy, CRA, MR;
var outtxt = "";
function setup() {
createCanvas(500, 500);
link_info();
}
function draw() {
background(120, 120, 0);
draw_points();
calc_circle();
draw_circum_circle();
text_info();
}
function draw_points() {
stroke(0);
noFill();
circle(x1, y1, d);
text("1", x1 + 3, y1 - 3);
circle(x2, y2, d);
text("2", x2 + 3, y2 - 3);
x3 = mouseX;
y3 = mouseY;
circle(x3, y3, d);
text("3", x3 + 3, y3 - 3);
stroke(0,0,200);
triangle(x1, y1, x2, y2, x3, y3);
}
function calc_circle() {
L12 = dist(x1, y1, x2, y2);
L23 = dist(x2, y2, x3, y3);
L31 = dist(x1, y1, x3, y3);
// now that is a SSS triangle, calc the angles
//A1 = acos((sq(L12) + sq(L31) - sq(L23)) / (2 * L12 * L31));
A2 = acos((sq(L12) + sq(L23) - sq(L31)) / (2 * L12 * L23));
//A3 = acos((sq(L23) + sq(L31) - sq(L12)) / (2 * L23 * L31));
//All = A1+A2+A3;
// calc middle points
// M12x = x1 + (x2 - x1) / 2;
// M12y = y1 + (y2 - y1) / 2;
// M23x = x2 + (x3 - x2) / 2;
// M23y = y2 + (y3 - y2) / 2;
M31x = x3 + (x1 - x3) / 2;
M31y = y3 + (y1 - y3) / 2;
//the common angle of departure being 90° minus the angle of the opposite vertex
// for CR must be: L31/2 = CR*cos(90-A2);
//CR = L31 / 2 / cos(HALF_PI-A2);
// now read WIKI: sinus law! 2CR = L31/sin(A2)
CR = L31 / 2 / sin(A2);
// MR = CR * sin(HALF_PI - A2);
CRA = acos((x3 - x1) / L31) - (HALF_PI - A2); // angle p1 p3 line - ( 90 - p2 angle)
CRx = x1 + CR * cos(CRA);
CRy = y1 + CR * sin(CRA);
}
function draw_circum_circle() {
stroke(255, 0, 0); // midpoints
// ellipse(M12x, M12y, r, r);
// ellipse(M23x, M23y, r, r);
circle(M31x, M31y, d);
stroke(255, 0, 0);
fill(130, 130, 0);
triangle(x1, y1, CRx, CRy, M31x, M31y); // one of 6 magic triangles what rule the circum circle
noFill();
stroke(200, 200, 0); // yellow
circle(CRx, CRy, 2 * CR); // circum circle
circle(CRx, CRy, d); // centerpoint
}
function text_info() {
noStroke();
fill(255);
outtxt = " CR: " + nf(CR, 0, 1);
outtxt += " , CRA: " + nf(degrees(CRA), 0, 1);
outtxt += " deg, CRx " + nf(CRx, 0, 0);
outtxt += " , CRy " + nf(CRy, 0, 0);
text(outtxt, 10, height - 10);
outtxt = " L12: " + nf(L12, 0, 0);
outtxt += " , L23: " + nf(L23, 0, 0);
outtxt += " , L31: " + nf(L31, 0, 0);
outtxt += " , x3: " + nf(x3, 0, 0);
outtxt += " , y3: " + nf(y3, 0, 0);
// outtxt += " , A1: " + nf(degrees(A1), 0, 1) + " deg ";
outtxt += " , A2: " + nf(degrees(A2), 0, 1) + " deg ";
// outtxt += " , A3: " + nf(degrees(A3), 0, 1) + " deg ";
// outtxt += " , All: " + nf(degrees(All), 0, 1) + " deg ";
text(outtxt, 10, 14);
}
function link_info() {
print(" org: https://editor.p5js.org/kll/sketches/rkRiXYYh7");
print(" math: https://en.wikipedia.org/wiki/Circumscribed_circle");
print(" wiki: https://en.wikipedia.org/wiki/Law_of_sines");
print(" forum: https://discourse.processing.org/t/circumcircle-of-triangle/13030");
}
/*
// processing 3.5.3 JAVA MODE version
int d = 7;
int x1 = 80, y1 = 60;
int x2 = 250,y2 = 30;
// mouse point for circum_circle play
float x3 = 350,y3 = 250; // about if y3 < y1 ERROR in calc
float L12, L23, L31, All, A1, A2, A3, M12x, M12y, M23x, M23y, M31x, M31y;
float CR, CRx, CRy, CRA, MR;
String outtxt = "";
void setup() {
size(500, 500);
link_info();
}
void draw() {
background(120, 120, 0);
draw_points();
calc_circle();
draw_circum_circle();
text_info();
}
void draw_points() {
stroke(0);
noFill();
circle(x1, y1, d);
text("1", x1 + 3, y1 - 3);
circle(x2, y2, d);
text("2", x2 + 3, y2 - 3);
x3 = mouseX;
y3 = mouseY;
circle(x3, y3, d);
text("3", x3 + 3, y3 - 3);
stroke(0,0,200);
triangle(x1, y1, x2, y2, x3, y3);
}
void calc_circle() {
L12 = dist(x1, y1, x2, y2);
L23 = dist(x2, y2, x3, y3);
L31 = dist(x1, y1, x3, y3);
// now that is a SSS triangle, calc the angles
//A1 = acos((sq(L12) + sq(L31) - sq(L23)) / (2 * L12 * L31));
A2 = acos((sq(L12) + sq(L23) - sq(L31)) / (2 * L12 * L23));
//A3 = acos((sq(L23) + sq(L31) - sq(L12)) / (2 * L23 * L31));
//All = A1+A2+A3;
// calc middle points
// M12x = x1 + (x2 - x1) / 2;
// M12y = y1 + (y2 - y1) / 2;
// M23x = x2 + (x3 - x2) / 2;
// M23y = y2 + (y3 - y2) / 2;
M31x = x3 + (x1 - x3) / 2;
M31y = y3 + (y1 - y3) / 2;
//the common angle of departure being 90° minus the angle of the opposite vertex
// for CR must be: L31/2 = CR*cos(90-A2);
//CR = L31 / 2 / cos(HALF_PI-A2);
// now read WIKI: sinus law! 2CR = L31/sin(A2)
CR = L31 / 2 / sin(A2);
// MR = CR * sin(HALF_PI - A2);
CRA = acos((x3 - x1) / L31) - (HALF_PI - A2); // angle p1 p3 line - ( 90 - p2 angle)
CRx = x1 + CR * cos(CRA);
CRy = y1 + CR * sin(CRA);
}
void draw_circum_circle() {
stroke(255, 0, 0); // midpoints
// ellipse(M12x, M12y, r, r);
// ellipse(M23x, M23y, r, r);
circle(M31x, M31y, d);
stroke(255, 0, 0);
fill(130, 130, 0);
triangle(x1, y1, CRx, CRy, M31x, M31y); // one of 6 magic triangles what rule the circum circle
noFill();
stroke(200, 200, 0); // yellow
circle(CRx, CRy, 2 * CR); // circum circle
circle(CRx, CRy, d); // centerpoint
}
void text_info() {
noStroke();
fill(255);
outtxt = " CR: " + nf(CR, 0, 1);
outtxt += " , CRA: " + nf(degrees(CRA), 0, 1);
outtxt += " deg, CRx " + nf(CRx, 0, 0);
outtxt += " , CRy " + nf(CRy, 0, 0);
text(outtxt, 10, height - 10);
outtxt = " L12: " + nf(L12, 0, 0);
outtxt += " , L23: " + nf(L23, 0, 0);
outtxt += " , L31: " + nf(L31, 0, 0);
outtxt += " , x3: " + nf(x3, 0, 0);
outtxt += " , y3: " + nf(y3, 0, 0);
// outtxt += " , A1: " + nf(degrees(A1), 0, 1) + " deg ";
outtxt += " , A2: " + nf(degrees(A2), 0, 1) + " deg ";
// outtxt += " , A3: " + nf(degrees(A3), 0, 1) + " deg ";
// outtxt += " , All: " + nf(degrees(All), 0, 1) + " deg ";
text(outtxt, 10, 14);
}
void link_info() { // with processing IDE JAVA MODE links not work in console
println(" org: https://editor.p5js.org/kll/sketches/rkRiXYYh7");
println(" math: https://en.wikipedia.org/wiki/Circumscribed_circle");
println(" wiki: https://en.wikipedia.org/wiki/Law_of_sines");
println(" forum: https://discourse.processing.org/t/circumcircle-of-triangle/13030");
}
*/