[Tex/LaTex] Using calc: Draw tangent lines to an ellipse path by tikz

tikz-pgf

I wonder if one can draw tangent lines out of a point to a path of an ellipse using tikz. Actually for the case of circle paths this can be accomplished easily as given in the tikz-pgf manual, but for other paths it wouldn't be an easy task.

I have tried to solve this problem with the help of some threads, but my solution, unfortunately, relies on some mathematical background. I wonder if I could solve the same problem with pure tikz.

Here is my solution:

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\usetikzlibrary{calc,intersections,arrows}
\usepackage{times}
\begin{document}

\begin{tikzpicture}[scale=1,>=latex',scale=1.5,outer sep=0,inner sep=0,line width=0.7pt]

\def\a{2} \def\b{1}    % radii of Ellipse
\def\cx{0} \def\cy{0}  % determines center of Ellipse
\def\xp{0} \def\yp{-5} % coordinates of point P

\coordinate (P) at (\xp,\yp);
\coordinate (A) at (0,\b);   % up
\coordinate (B) at (0,-\b);  % down
\coordinate (C) at (\a,0);   % right

\draw[name path=ellipse,fill=gray!30,opacity=0.5](\cx,\cy)circle[x radius=\a,y radius=\b];

\path[name path=linePC] (P)--(C);
\path[name path=linePC] (P)--(C);

\path [name intersections={of = ellipse and linePC}];
\coordinate (E) at (intersection-2);
\coordinate (F) at (intersection of A--C and B--E);
\path let \p1=(F) in node (G) at (-1.2*\a,\y1){};

\path[name path=lineFG] (F)--(G);
\path [name intersections={of = ellipse and lineFG}];
\coordinate (X) at (intersection-1);
\coordinate (Y) at (intersection-2);
\draw (X)--(P)--(Y);



%%%%%%%%%%%%%%%%%%%%%% Second Ellipse%%%%%%%%%%%%%%
\def\a{1} \def\b{0.5}  % radii of Ellipse
\def\cx{0} \def\cy{0}  % determines center of Ellipse
\def\xp{0} \def\yp{-5} % coordinates of point P

\coordinate (P) at (\xp,\yp);
\coordinate (A) at (0,\b);   % up
\coordinate (B) at (0,-\b);  % down
\coordinate (C) at (\a,0);   % right

\draw[name path=ellipse,fill=white](\cx,\cy)circle[x radius=\a,y radius=\b];

\path[name path=linePC] (P)--(C);
\path[name path=linePC] (P)--(C);

\path [name intersections={of = ellipse and linePC}];
\coordinate (E) at (intersection-2);
\coordinate (F) at (intersection of A--C and B--E);
\path let \p1=(F) in node (G) at (-1.2*\a,\y1){};

\path[name path=lineFG] (F)--(G);
\path [name intersections={of = ellipse and lineFG}];
\coordinate (X) at (intersection-1);
\coordinate (Y) at (intersection-2);
\draw (X)--(P) (P)--(Y);

\draw [->,thin](0,-5.5)--(0,1.5) node[above]{$z$};
\draw [<-,shorten <=0.2pt,thin](P)--+(0.4,-0.1) node[right]{Gap};

\draw [->,thin](0,-2.2) arc (90:65.4:2.5);     
\node [fill=white] at (0.5,-2.5){$\theta_2$};
\draw [->,thin](0,-3.5) arc (90:79:1.6);     
\node  at (0.13,-3.7){$\theta_1$};

\end{tikzpicture}

\end{document}

I appreciate your help and thank you in advance.

Best Answer

If we were dealing with circles, then the radius would be perpendicular to the tangent, so we would need to find a right triangle connecting the center C, point P, and the circle. If you recall your geometry, any triangle inscribed in a circle where one side of the triangle passes through the center will form a right triangle. So if we construct a circle whose center is the midpoint between P and C, the intersection of the two circles is the point(s) of tangency.

Since ellipses can be thought of as circles seen from an angle, instead of a circle we need an ellipse with the same aspect ratio.

For the purposes of illustration, I made the construct visible (pink).

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\usetikzlibrary{calc,intersections,arrows}
\usepackage{times}
\begin{document}

\begin{tikzpicture}[scale=1,>=latex',scale=1.5,outer sep=0,inner sep=0,line width=0.7pt]

\def\a{2} \def\b{1}    % radii of Ellipse
\def\cx{0} \def\cy{0}  % determines center of Ellipse
\def\xp{0} \def\yp{-5} % coordinates of point P

\coordinate (C) at (\cx,\cy);% center of ellipse
\coordinate (P) at (\xp,\yp);% focal point

\draw[name path=ellipse,fill=gray!30,opacity=0.5](C)circle[x radius=\a,y radius=\b];

\pgfmathparse{abs(\cx-\xp)/\a}% compute normalized distance from C to P
\edef\dx{\pgfmathresult}
\pgfmathparse{abs(\cy-\yp)/\b}
\edef\dy{\pgfmathresult}
\pgfmathparse{0.5*ifthenelse(\dx,ifthenelse(\dy,sqrt(\dx*\dx+\dy*\dy),\dx),\dy)}
\edef\d{\pgfmathresult}% half the distance from C to P
\pgfmathparse{\a*\d}% compute ellipse radii
\edef\rx{\pgfmathresult}
\pgfmathparse{\b*\d}
\edef\ry{\pgfmathresult}

\draw[color=pink,name path=construct] ($(C)!0.5!(P)$) circle[x radius=\rx, y radius=\ry];
\path [name intersections={of = ellipse and construct}];
\coordinate (X) at (intersection-1);
\coordinate (Y) at (intersection-2);

\draw (X)--(P)--(Y);

\end{tikzpicture}
\end{document}

tangents