I've written a small sketch which calculates the circumcircle for any given triangle. The code as it is works fine.
There are three custom functions. Into the first and second function go the x- and y-coordinates from the three corner-points of the triangle. These return the x and y for the circumcenter. Then I pass these into the third function to get the circumradius.
Code:
// calculates the circumcircle
// april 3, 2008 | greg fraser
// click to redraw!
float x, y, r;
float u, v, d;
float x1, y1;
float x2, y2;
float x3, y3;
int big = 10;
void setup() {
size(400,400);
smooth();
setThreePoints();
// here I call the functions which calculate the x, y and radius of the circumcircle
x = circumX(x1,y1,x2,y2,x3,y3);
y = circumY(x1,y1,x2,y2,x3,y3);
r = circumR(x,y,x1,y1);
}
void draw() {
background(0);
// cornerpoints
noStroke();
fill(255);
ellipse(x1,y1,big,big);
ellipse(x2,y2,big,big);
ellipse(x3,y3,big,big);
// triangle
stroke(255);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
// circumcircle
noFill();
stroke(255,0,0);
ellipse(x,y,2*r,2*r);
}
float circumX(float _x1, float _y1, float _x2, float _y2, float _x3, float _y3) {
u = (_y2-_y3)*(sq(_x1)+sq(_y1)) + (_y3-_y1)*(sq(_x2)+sq(_y2)) + (_y1-_y2)*(sq(_x3)+sq(_y3));
d = _x1*_y2 + _x2*_y3 + _x3*_y1 - _x1*_y3 - _x2*_y1 - _x3*_y2;
float tx = u/d*.5;
return tx;
}
float circumY(float _x1, float _y1, float _x2, float _y2, float _x3, float _y3) {
v = (_x3-_x2)*(sq(_x1)+sq(_y1)) + (_x1-_x3)*(sq(_x2)+sq(_y2)) + (_x2-_x1)*(sq(_x3)+sq(_y3));
d = _x1*_y2 + _x2*_y3 + _x3*_y1 - _x1*_y3 - _x2*_y1 - _x3*_y2;
float ty = v/d*.5;
return ty;
}
float circumR(float _x, float _y, float _x1, float _y1) {
float tr = sqrt(sq(_x-_x1)+sq(_y-_y1));
return tr;
}
void mousePressed() {
setThreePoints();
x = circumX(x1,y1,x2,y2,x3,y3);
y = circumY(x1,y1,x2,y2,x3,y3);
r = circumR(x,y,x1,y1);
}
void setThreePoints() {
// set three points
x1 = random(width);
y1 = random(height);
x2 = random(width);
y2 = random(height);
x3 = random(width);
y3 = random(height);
}
I feel this isn't the most elegant way to do this, since I create functions, get return values and put these into another custom function. I believe this should be possible to do in a single pass, send the cornerpoint-coordinates in, have the math do the magic, and then have x, y and r ready for read-out.
I'm imagining something like a circumcircle-calculator-object. Or even better, a package which, based on three coordinates, can calculate various useful values in and around the triangle. Anyone got a suggestion on where I could get started on this?
cheers.