Demo
Click on the panel below to see the demo
Steps of the algorithm
We all know that the circumcircle of a triangle is the intersection of the three perpendicular bisectors of it. Based on this property, we can calculate the coordinates of the center of the circumcircle of triangle according to the following steps:
Find the midpoints and of sides and respectively
Calculate the equation of the line perpendicular to and at the midpoints and just found
Find the intersection of these two perpendicular lines.
Find the mid point
Given two points , the mid point of the line segment is calculated as
Find the perpendicular vectors to a line
Given a line defined by 2 points: . There are 2 perpendicular vectors to the line: with then we have
Code
// find perpendicular vector of P1 P2
function perpendicularOfALine(p1, p2){
const v = createVector(p2.x - p1.x, p2.y - p1.y).normalize();
const pVector1 = createVector(v.y, -v.x);
const pVector2 = createVector(-v.y, v.x);
return [pVector1, pVector2];
}
Find intersection of 2 lines
Given 2 points we calculate the formula of the line go through those points in form by:
We have the formula to calculate the intersection of two lines and is as follow:
Code
// find intersection of 2 lines
function findIntersectionOfTwoLines(p1, p2, q1, q2){
const a1 = p2.y - p1.y;
const b1 = p1.x - p2.x;
const c1 = -(a1 * p1.x + b1 * p1.y);
const a2 = q2.y - q1.y;
const b2 = q1.x - q2.x;
const c2 = -(a2 * q1.x + b2 * q1.y);
const det = a1 * b2 - a2 * b1;
if (det == 0){
return createVector(0, 0);
} else {
const x = (b1 * c2 - b2 * c1) / det;
const y = (a2 * c1 - a1 * c2) / det;
return createVector(x, y);
}
}