Find circumcircle of a triangle

 

Click on the panel below to see the demo

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 M1 and M2 of sides AB and AC respectively

  • Calculate the equation of the line perpendicular to AB and AC at the midpoints M1 and M2 just found

  • Find the intersection of these two perpendicular lines.

Given two points A(xA,yA),B(xB,yB), the mid point of the line segment AB is calculated as M(xM,yM)=M(xA+xB2,yA+yB2)

Given a line defined by 2 points: A(xA,yA),B(xB,yB). There are 2 perpendicular vectors to the line: v1AB,v2AB with AB=(xBxA,yByA) then we have v1=(yByA,xBxA),v2=(yAyB,xAxB)

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

				
			

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{a=yByAb=xAxBc=a×xAb×yB

We have the formula to calculate the intersection of two lines a1x+b1y+c1=0 and a2x+b2y+c2=0 is as follow:

(x,y)=(b1×c2b2×c1a1×b2a2×b1,c1×a2c2×a1a1×b2a2×b1)

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

				
			

Subscribe to SkyGLab

Scroll to Top