0

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?

Federico
  • 201
  • I don't understand what Because I need to find the slope of the tangent so that is always oriented away from the circle. Can you give a precise meaning to that sentence in term of geometrical words? – mathcounterexamples.net Mar 12 '21 at 15:30
  • Sorry, I still don't get it. And I have the feeling that you'll find an answer to your own question if first, you clarify your mind on what you expect! To be a bit straight... trash in your question the program and the computed figures and make simple hand drawings making explicit what you expect. From there, you can derive the program. – mathcounterexamples.net Mar 12 '21 at 15:43
  • See the answer below. Sorry to say it again you obfuscated your question with a lot of elements that are not relevant to get an answer. – mathcounterexamples.net Mar 12 '21 at 16:04
  • 1
    To clarify, there is a circle, an external point, and two tangent points. You want to quantify the direction of travel heading away from one of the two tangent points? And probably you select the tangent point based on whether it is "on the left" or "on the right"? – D. G. Mar 12 '21 at 16:45
  • @D.G. correct, away from one of the two tangent points and towards the external one. yes on the selection, it is done by the $\mp$ in the second component of the vector being multiplied – Federico Mar 12 '21 at 16:48
  • @D.G. This is not my understanding of the question. It seems that one of the two tangents has already been selected. And then, for the selected tangent, Federico is expecting to select the orientation of that tangent in a way that moving in the positive parametric direction from the external point you move away from the tangent point. I let Federico confirm or not this interpretation... and update again its question if necessary. – mathcounterexamples.net Mar 12 '21 at 17:41
  • I removed the approximation part, and reorganised the explanation. Let me know if it still doesn't make sense or doesn't work. – D. G. Mar 13 '21 at 12:44

1 Answers1

1

Expanding on my comment, there is a circle with centre $C$ and radius $r$, an external point $P$, and two tangent points $T_+$ (to the right from $P$, anti-clockwise around $C$) and $T_-$ (to the left from $P$, clockwise around $C$). (Note: we are using the standard coordinate system with $\theta = 0$ starting at the $x$-axis and increasing anti-clockwise.) You want to quantify the direction of travel $\alpha_\pm$ heading away from $T_\pm$.

First we find the tangent points $$ T_\pm = C + \underbrace{\frac{r}{\Vert P - C \Vert}}_{\text{scale}} \cdot \underbrace{R(\pm\theta)}_{\text{rotate}} \cdot (P - C), $$ where $$ R(\pm\theta) = \begin{bmatrix} \cos\theta & \mp\sin\theta \\ \pm\sin\theta & \cos\theta \end{bmatrix} $$ is the general rotation matrix and $$ \cos\theta = \frac{r}{\Vert P - C \Vert}, \quad \sin\theta = \sqrt{1 - \cos^2 \theta}, \quad \left(0 \le \theta \le \frac{\pi}{2}\right) $$ by observing the right triangle $C T_+ P$. Then the required heading is $$ \alpha_\pm = \text{atan2}(P - T_{\pm}). $$


We can also make the formula more compact. Let $$ \lambda = \tan\theta = \frac{\sin\theta}{\cos\theta} = \sqrt{\frac{\Vert P - C \Vert^2}{r^2} - 1}, $$ then some algebraic manipulation gives $$ \alpha_\pm = \text{atan2}\left( \begin{bmatrix} \lambda & \mp 1 \\ \pm 1 & \lambda \end{bmatrix} (P - C) \right). $$

D. G.
  • 330
  • 1
  • 8