Given a circle (defined through its center $(x_c,y_c)$ and radius $r$) and an external point $(x_0, y_0)$ , I am looking for the heading of one of the two tangents (I know how to select the one I need case-by-case) such that if I write the tangent line in the parametric form
$$ x(t) = x_0 + t \cdot \sin(heading) $$ $$ y(t) = y_0 +t \cdot \cos(heading) \, , $$ I want the tangent point to be in the range $t<0$.
NOTE: the parametric equation is given to explain the desired orientation of the heading. I need the heading defined in that way.
I have read this answer and it mostly works fine, but there are some cases in my application where the function given provides $heading +\pi$:
$$vec = [r:\mp\sqrt{(y_0 - y_c)^2+(x_0 - x_c)^2-r^2}:-\sqrt{(y_0 - y_c)^2+(x_0 - x_c)^2}] \begin{bmatrix}(y_0 - y_c)&(x_0 - x_c)&-x_c(y_0 - y_c)-y_c(x_0 - x_c)\\ -(x_0 - x_c)&(y_0 - y_c)&x_c(x_0 - x_c)-y_c(y_0 - y_c) \\ 0&0&r\sqrt{(y_0 - y_c)^2+(x_0 - x_c)^2}\end{bmatrix}$$
$$heading = \arctan_2(vec(1),vec(2))$$
Question: how can I fix this function so that I always obtain the desired value for the heading?