← Back to index

Tangent lines from an external point

From any point outside a circle there are exactly two tangent lines. At each tangent point T, the radius CT is perpendicular to the tangent PT, so ∠PCT has cosine r/d. Move the mouse around the circle.

See also: Circumference point — the tangent points are circumference points at offset angles ±acos(r/d).

Pseudocode

d = distance(p, c)
if (d < r) return []             // point inside
theta = atan2(p.y - c.y, p.x - c.x)
alpha = acos(r / d)
t1 = c + r * (cos(theta + alpha), sin(theta + alpha))
t2 = c + r * (cos(theta - alpha), sin(theta - alpha))

Source

const cx = 150, cy = 150, r = 60

const dx = mouseX - cx
const dy = mouseY - cy
const d = Math.sqrt(dx * dx + dy * dy)

if (d >= r) {
    const theta = Math.atan2(dy, dx)
    const alpha = Math.acos(r / d)
    const t1x = cx + r * Math.cos(theta + alpha)
    const t1y = cy + r * Math.sin(theta + alpha)
    const t2x = cx + r * Math.cos(theta - alpha)
    const t2y = cy + r * Math.sin(theta - alpha)
}