← Back to index

Incircle & circumcircle together

The incircle sits inside the triangle and touches each edge; the circumcircle sits outside and passes through each vertex. Their centres (the incenter I and circumcenter O) only coincide for an equilateral triangle, where the ratio R / r reaches its minimum of 2 (Euler's inequality). Two vertices are fixed; the third follows the mouse.

See also: Triangle incircle and Circumcircle in isolation.

Pseudocode

// Incircle
edges     = (|B-C|, |C-A|, |A-B|) = (a, b, c)
incenter  = (aA + bB + cC) / (a + b + c)
inradius  = area / ((a + b + c) / 2)

// Circumcircle
d = 2 * (A.x*(B.y-C.y) + B.x*(C.y-A.y) + C.x*(A.y-B.y))
ox = (...) / d                     // see circumcircle demo
oy = (...) / d
R  = |O - A|

Source

// Incircle
const a = Math.hypot(B.x - C.x, B.y - C.y)
const b = Math.hypot(C.x - A.x, C.y - A.y)
const c = Math.hypot(A.x - B.x, A.y - B.y)
const p = a + b + c
const ix = (a*A.x + b*B.x + c*C.x) / p
const iy = (a*A.y + b*B.y + c*C.y) / p
const area = Math.abs((B.x - A.x)*(C.y - A.y) - (C.x - A.x)*(B.y - A.y)) / 2
const r = area / (p / 2)

// Circumcircle (perpendicular-bisector formula)
const d  = 2 * (A.x*(B.y - C.y) + B.x*(C.y - A.y) + C.x*(A.y - B.y))
const sa = A.x*A.x + A.y*A.y
const sb = B.x*B.x + B.y*B.y
const sc = C.x*C.x + C.y*C.y
const ox = (sa*(B.y - C.y) + sb*(C.y - A.y) + sc*(A.y - B.y)) / d
const oy = (sa*(C.x - B.x) + sb*(A.x - C.x) + sc*(B.x - A.x)) / d
const R  = Math.hypot(A.x - ox, A.y - oy)