Search code examples
algorithmmathgeometryalgebra

Find tangent points in a circle from a point


Circle center : Cx,Cy

Circle radius : a

Point from which we need to draw a tangent line : Px,Py

I need the formula to find the two tangents (t1x, t1y) and (t2x,t2y) given all the above.

Edit: Is there any simpler solution using vector algebra or something, rather than finding the equation of two lines and then solving equation of two straight lines to find the two tangents separately? Also this question is not off-topic because I need to write a code to find this optimally


Solution

  • Here's another way using complex numbers. If a is the direction (a complex number of length 1) of the tangent point on the circle from the centre c, and d is the (real) length along the tangent to get to p, then (because the direction of the tangent is I*a)

    p = c + r*a + d*I*a 
    

    rearranging

    (r+I*d)*a = p-c
    

    But a has length 1 so taking the length we get

    |r+I*d| = |p-c|
    

    We know everything but d, so we can solve for d:

    d = +- sqrt( |p-c|*|p-c| - r*r)
    

    and then find the a's and the points on the circle, one of each for each value of d above:

    a = (p-c)/(r+I*d)
    q = c + r*a