← Back to index

Reflect a point across a line

Project the point onto the line to get the foot of the perpendicular, then go the same distance again on the other side: p' = 2·foot − p.

See also: Reflect a line off another line — the same idea applied to a direction vector instead of a position.

Pseudocode

ab    = b - a
ap    = p - a
t     = (ap . ab) / (ab . ab)
foot  = a + ab * t
pRefl = foot * 2 - p

Source

const ax = 40,  ay = 250
const bx = 260, by = 50

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

const t = (apx * abx + apy * aby) / (abx * abx + aby * aby)
const footX = ax + abx * t
const footY = ay + aby * t

const reflX = footX * 2 - mouseX
const reflY = footY * 2 - mouseY