← Back to index

Triangle incircle

The largest circle that fits inside a triangle — the incircle — touches each edge at exactly one point. Its centre, the incenter, is the weighted average of the vertices using opposite-edge lengths as weights. Its radius is the triangle's area divided by its semiperimeter. Two vertices are fixed; the third follows the mouse.

See also: Circumcircle — the other classical triangle circle, through all three vertices instead of touching all three edges; Incircle & circumcircle together.

Pseudocode

// Edge lengths (opposite to each vertex)
a = |B - C|
b = |C - A|
c = |A - B|

perimeter   = a + b + c
incenter    = (a*A + b*B + c*C) / perimeter
semi        = perimeter / 2
inradius    = area / semi

Source

const A = { x:  60, y: 240 }
const B = { x: 240, y: 240 }
const C = { x: mouseX, y: mouseY }

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 perimeter = a + b + c
const ix = (a*A.x + b*B.x + c*C.x) / perimeter
const iy = (a*A.y + b*B.y + c*C.y) / perimeter

// area via shoelace
const area = Math.abs((B.x - A.x) * (C.y - A.y) - (C.x - A.x) * (B.y - A.y)) / 2
const r    = area / (perimeter / 2)