Find circumcircle of a triangle

Find circumcircle of a triangle

This is a preparation note for Delauney Triangulation algorithm note which I will write later!

Demo

Click on the panel below to see the demo

Steps of the algorithm

We all know that the circumcircle of a triangle \(ABC\) 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 \(ABC\) according to the following steps:

  • Find the midpoints \(M_1\) and \(M_2\) of sides \(AB\) and \(AC\) respectively

  • Calculate the equation of the line perpendicular to \(AB\) and \(AC\) at the midpoints \(M_1\) and \(M_2\) just found

  • Find the intersection of these two perpendicular lines.

Find the mid point

Given two points \(A(x_A, y_A), B(x_B,y_B)\), the mid point of the line segment \(AB\) is calculated as \(\displaystyle M(x_M,y_M) = M(\frac {x_A+x_B} 2, \frac {y_A+y_B} 2)\)

Find the perpendicular vectors to a line

Given a line defined by 2 points: \(A(x_A, y_A), B(x_B,y_B)\). There are 2 perpendicular vectors to the line: \(v_1 \perp \overrightarrow{AB}, v_2 \perp \overrightarrow{AB}\) with \(\overrightarrow{AB}=(x_B-x_A, y_B-y_A)\) then we have \(v1=(y_B-y_A,x_B-x_A), v_2=(y_A-y_B,x_A-x_B)\)

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 \(A,B\) we calculate the formula of the line go through those points in form \(ax+by+c=0\) by:

$$(d): ax+by+c = 0 \implies \begin{cases} a = y_B - y_A \\ b = x_A - x_B \\ c = -a \times x_A - b \times y_B \end{cases}$$

We have the formula to calculate the intersection of two lines \(a_1x+b_1y+c_1 = 0\) and \(a_2x+b_2y+c_2 = 0\) is as follow:

$$(x,y) = \left(\frac {b_1\times c_2 - b_2 \times c_1} {a_1 \times b_2 - a_2 \times b_1}, \frac {c_1\times a_2 - c_2 \times a_1} {a_1 \times b_2 - a_2 \times b_1} \right)$$

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);
    }    
}