← Back to index

Circumcircle of three points

Three non-collinear points define a unique circle (the circumcircle of the triangle). The centre is the intersection of the perpendicular bisectors of any two sides. Two points are fixed; the third follows the mouse.

Pseudocode

d = 2 * (ax*(by-cy) + bx*(cy-ay) + cx*(ay-by))
if (d == 0) return null    // collinear

sa = ax*ax + ay*ay
sb = bx*bx + by*by
sc = cx*cx + cy*cy

ux = (sa*(by-cy) + sb*(cy-ay) + sc*(ay-by)) / d
uy = (sa*(cx-bx) + sb*(ax-cx) + sc*(bx-ax)) / d
r  = distance((ux, uy), (ax, ay))

Source

const ax = 60,  ay = 230
const bx = 240, by = 230
const cx = mouseX, cy = mouseY

const d = 2 * (ax * (by - cy) + bx * (cy - ay) + cx * (ay - by))
const sa = ax * ax + ay * ay
const sb = bx * bx + by * by
const sc = cx * cx + cy * cy

const ux = (sa * (by - cy) + sb * (cy - ay) + sc * (ay - by)) / d
const uy = (sa * (cx - bx) + sb * (ax - cx) + sc * (bx - ax)) / d

const dx = ax - ux, dy = ay - uy
const r = Math.sqrt(dx * dx + dy * dy)