← Back to index

Closest point on a line segment

Project the point onto the line, then clamp the parameter to [0, 1] so the result stays on the segment.

See also: Perpendicular distance from a point to a line — same projection without the clamp.

Pseudocode

ab  = b - a
ap  = p - a
t   = clamp((ap . ab) / (ab . ab), 0, 1)
cp  = a + ab * t

Source

const ax = 50,  ay = 50
const bx = 260, by = 220

const abx = bx - ax
const aby = by - ay
const apx = mouseX - ax
const apy = mouseY - ay

const lenSq = abx * abx + aby * aby
let t = (apx * abx + apy * aby) / lenSq
t = Math.max(0, Math.min(1, t))   // clamp to segment

const cx = ax + abx * t
const cy = ay + aby * t