Given a circle with known center $c$, known radius $r$ and perimeter point $x$: $$ (x - c_x)^2 + (y - c_y)^2 = r^2 $$ with a tangent line that also goes through a point $p$ lying outside the circle. How do I find the point $x$ at which the line touches the circle?
Given that the tangent line is orthogonal to the vector $(x-c)$ and also that the vector $(x-p)$ lies on the tangent line we have $(x-c) \cdot (x-p) = 0$ which can be expanded to:
$$ (x - c_x) (x - p_x) + (y - c_y) (y - p_y) = 0 $$
Thus my question is: How do I find the point $x$?
Inspired by the visual proof supplied by angryavian I have derived this proof:
Lets define the middle point between $c$ and $p$: $$ m = ((c - p) / 2) + p $$
And the distance from $m$ to $p$: $$ r_m = dist(m - p) $$
Now we can define the circle with center $m$ and radius $r_m$: $$ (x - m_x)^2 + (y - m_y)^2 = r_m^2 $$
This gives us two equations both containing the unknown variables $x$ and $y$. First we transform both equations to be equal to zero: $$ (x - c_x)^2 + (y - c_y)^2 - r^2 = 0 $$
$$ (x - m_x)^2 + (y - m_y)^2 - r_m^2 = 0 $$
Then we equal the two equations to each other: $$ (x - c_x)^2 + (y - c_y)^2 - r^2 = (x - m_x)^2 + (y - m_y)^2 - r_m^2 $$
Now we isolate $x$: $$ x = \dfrac {c_x^2-c_x p_x+c_y^2-c_y p_y-c_y y+p_y y-r^2}{c_x-p_x} $$
Inserting this equation into the equation for the circle with center $c$ and solving for $y$ we get two solutions: $$ y_1 = \dfrac {c_y k + r^2 (p_y - c_y) + \sqrt{-r^2 + k} \cdot r (c_x - p_x)} {k} $$ $$ y_2 = \dfrac {c_y k + r^2 (p_y - c_y) - \sqrt{-r^2 + k} \cdot r (c_x - p_x)} {k} $$
Where $k = (c_x - p_x)^2 + (c_y - p_y)^2$
These can then be inserted into the equation for $x$ to derive the corresponding solutions for $x$: $$ x_1 = \dfrac {c_x^2-c_x p_x+c_y^2-c_y p_y-c_y \left( \dfrac {c_y k + r^2 (p_y - c_y) + \sqrt{-r^2 + k} \cdot r (c_x - p_x)} {k} \right) +p_y \left( \dfrac {c_y k + r^2 (p_y - c_y) + \sqrt{-r^2 + k} \cdot r (c_x - p_x)} {k} \right)-r^2}{c_x-p_x} $$
and: $$ x_2 = \dfrac {c_x^2-c_x p_x+c_y^2-c_y p_y-c_y \left( \dfrac {c_y k + r^2 (p_y - c_y) - \sqrt{-r^2 + k} \cdot r (c_x - p_x)} {k} \right) +p_y \left( \dfrac {c_y k + r^2 (p_y - c_y) - \sqrt{-r^2 + k} \cdot r (c_x - p_x)} {k} \right)-r^2}{c_x-p_x} $$
Which can be shortened to: $$ x_1 = \dfrac {c_x k + r^2 (p_x - c_x) - \sqrt{-r^2 + k} \cdot r (c_y - p_y)} {k} $$ $$ x_2 = \dfrac {c_x k + r^2 (p_x - c_x) + \sqrt{-r^2 + k} \cdot r (c_y - p_y)} {k} $$