Given a circle with it's center point , the radius and an angle of the radius line, how can one calculate the tangent line on the circle in point ? The trick is, that the radius line and the tangent line are perpendicular. This means, that their dot-product must be zero.
So, how can this schematic be derived? The intersection point is the center point plus the radius in the direction of the angle:
Now we need to rotate the vector by 90° in order to get a vector that is perpendicular to the radius line:
Which leads to the line equation:
The implementation is therefore also quite simple:
function tangentLine(M, r, alpha) {
T = {
x: M.x + Math.cos(alpha) * r,
y: M.y + Math.sin(alpha) * r
}
v = {
x: T.y - M.y,
y: M.x - T.x
}
norm = Math.hypot(v.x, v.y)
v.x/= norm
v.y/= norm
return [T, v]
}