[Tex/LaTex] Different Results for distance by sqrt and veclen

calculationstikz-pgf

I'm trying a simple task of drawing a tangent to a circle. While I already succeded with the tangent cs option, I also want to use the library calc for the exact intersection points. I simply start from the middle of the circle and calculate the respective angle to the intersection point by acos of the adjacent leg and the hypotenuse. The first one is the radius, the second one I want to have calculated by the calc library. This is what I tried:

\begin{tikzpicture}
\coordinate (A) at (13,9);
\coordinate (T) at (10,2);
%
\draw (T) circle (2);
\draw let \p1 = ($(A)-(T)$) in (T) --++ ({acos(divide(2,veclen(\x1,\y1)))}:2)--(A);
\draw (T) --++ ({acos(2/sqrt(3^2+7^2))}:2)--(A);
\draw (T) -- (A);
\end{tikzpicture}

However, neither of the two intersection points are correct. Why is that?

Best Answer

Like Alex wrote you need to add atan(7/3) but if you want to use veclen you also need to modify your expression to calculate the angle

Remark : compare the next codes (\x1 =85.35823pt \y1=199.1692pt)

\pgfmathparse{veclen(85.35823pt,199.1692pt)} \pgfmathresult
\pgfmathparse{veclen(3,7)} \pgfmathresult
\pgfmathparse{veclen(3cm,7cm)} \pgfmathresult

The pgfmanual gives only

veclen(x,y) \pgfmathveclen{x}{y} Calculates sqrt(x^2 + y^2). This uses a polynomial approximation, based on ideas of Rouben Rostamian

12.99976 result of \pgfmathparse{veclen(12,5)} \pgfmathresult

This is not enough. The dimensions are in pt. You have the possibility to give another unit for example \pgfmathparse{veclen(12cm,5cm)} \pgfmathresult and the result is 369.87305 but in pt. Possible also \pgfmathparse{veclen(12cm,5pt)} but it's a bad idea to mix units.

The complete code with veclen

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}    
\begin{document}  

\begin{tikzpicture}
\coordinate (A) at (13,9);
\coordinate (T) at (10,2);
%
\draw (T) circle (2);
\draw[red]  let \p1 = ($(A) - (T)$), \n1={veclen(\x1/28.45274,\y1/28.45274)} in 
          (T) -- ++({atan(\y1/\x1) - acos(divide(2,\n1))}:2) -- (A);

 % or  acos(divide(2cm,veclen(\x1,\y1)))}:2   
 %  instead of     acos(divide(2,veclen(\x1/28.45274,\y1/28.45274)))}:2  

\draw[red]  let \p1 = ($(A) - (T)$), \n1={veclen(\x1/28.45274,\y1/28.45274)} in 
          (T) -- ++({atan(\y1/\x1) + acos(divide(2,\n1))}:2) -- (A);

\draw (T) -- (A);
\end{tikzpicture} 

\end{document}

enter image description here

The simplest way with veclen

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

    \begin{tikzpicture}
    \coordinate (A) at (13,9);
    \coordinate (T) at (10,2);
    \draw (T) circle (2);
    \draw let \p1 = ($(A)-(T)$), \n1={acos(divide(2cm,veclen(\x1,\y1)))}
     in (T) -- ++ ({atan(\y1/\x1) - \n1}:2)--(A)
        (T) -- ++ ({atan(\y1/\x1) + \n1}:2)--(A);
    \draw (T) -- (A);
    \end{tikzpicture}

\end{document}