Skip to content

Instantly share code, notes, and snippets.

@rikusalminen
Last active March 1, 2017 09:25
Show Gist options
  • Save rikusalminen/854d05e27eb6dee7e682 to your computer and use it in GitHub Desktop.
Save rikusalminen/854d05e27eb6dee7e682 to your computer and use it in GitHub Desktop.
Elliptic anomaly in TikZ
% vim:nolist lbr tw=78 expandtab autoindent nocindent
\documentclass[a4paper]{minimal}
\usepackage[landscape,top=1cm,bottom=1cm,left=1cm,right=1cm]{geometry}
\usepackage{tikz}
%\usetikzlibrary{decorations.pathreplacing}
\everymath{\displaystyle} % set math in display style, not text style (ie. larger)
\begin{document}
\begin{tikzpicture}[scale=1.0]
\pgfmathsetmacro{\maxr}{7}
\pgfmathsetmacro{\p}{2}
\pgfmathsetmacro{\e}{0.7}
\pgfmathsetmacro{\a}{\p / (1 - \e^2)}
\pgfmathsetmacro{\b}{\p / sqrt(1 - \e^2)}
\pgfmathsetmacro{\c}{sqrt(\a^2 - \b^2)} % XXX: ellipse only!
%\pgfmathsetmacro{\c}{\a*\e}
\pgfmathsetmacro{\q}{\p / (1 + \e)}
\pgfmathsetmacro{\directrixx}{\p / \e}
\pgfmathsetmacro{\f}{115} % true anomaly in degrees
% atan2() argument order ?!
\pgfmathsetmacro{\E}{atan2(sqrt(1 - \e^2) * sin(\f), cos(\f) + \e)}
\pgfmathsetmacro{\Pr}{\p / (1 + \e * cos(\f))}
\pgfmathsetmacro{\Px}{\Pr * cos(\f)}
\pgfmathsetmacro{\Py}{\Pr * sin(\f)}
\pgfmathsetmacro{\Qx}{-\c + \a*cos(\E)}
\pgfmathsetmacro{\Qy}{\a * sin(\E)}
\coordinate (F) at (0,0);
\coordinate (C) at (-\c,0);
\coordinate (Ap) at (-\c - \a,0);
\coordinate (Pe) at (\q,0);
\coordinate (Mi) at (-\c,-\b);
\coordinate (Re) at (0,-\p);
\coordinate (P) at (\Px, \Py);
\coordinate (Q) at (\Qx, \Qy);
\coordinate (N) at (\directrixx, \Py);
% grid and axes
%\draw[gray,thin] (-\maxr,-\maxr) grid (\maxr,\maxr);
\draw[->, thin] (-\maxr, 0) -- (\maxr+0.2, 0)
node[right]{$x$};
\draw[->, thin] (0, -\maxr) -- (0, \maxr+0.2)
node[above]{$y$};
% conic section
\fill[color=gray, opacity=0.5, domain=0:rad(\f), samples=100, smooth]
(F) --
plot (xy polar cs:angle=\x r, radius={\p / (1 + \e * cos(\x r))});
\draw[thick, domain=-pi:pi, samples=200, smooth]
plot (xy polar cs:angle=\x r, radius={\p / (1 + \e * cos(\x r))});
% directrix
\draw[thick] (\directrixx,-\a) -- (\directrixx,\a)
node[above, color=black]{Directrix};
\draw[<->, thick] (0,-\p) -- (\directrixx,-\p)
node[below, midway]{$\frac{p}{e}$};
% conic section measurements (major axis, minor axis, latus rectum)
\draw[<->,thick] (C) -- (Ap)
node[below, midway]{$a$};
\draw[<->,thick] (C) -- (F)
node[below, midway]{$c$};
\draw[<->,thick] (C) -- (Mi)
node[left, midway]{$b$};
\draw[<->,thick] (F) -- (Re)
node[left, midway]{$p$};
\draw[<->,thick] (F) -- (Pe)
node[below, midway]{$q$};
% auxiliary circle
\draw[thin, dashed] (C) circle[radius={\a}];
% arrows from focus and center to orbit and aux circle
\draw[->, thick] (F) -- (P)
node[midway, anchor=east]{$r$}
node[anchor=south west]{$P$};
\draw[->, thick] (C) -- (Q)
node[midway, anchor=east]{$a$}
node[anchor=south west]{$Q$};
% arrow to directrix
\draw[<->, thick] (P) -- (N)
node[above, midway]{$\frac{r}{e}$};
% arcs for true and eccentric anomaly
\draw[->, thick]
(F) node[anchor=south west]{$f$}
++(0.7,0) arc [start angle=0, end angle={\f}, radius=0.7];
\draw[->, thick]
(C) node[anchor=south west]{$E$}
++(0.7,0) arc [start angle=0, end angle={\E}, radius=0.7];
\end{tikzpicture}
\begin{tikzpicture}[scale=1.0]
\pgfmathsetmacro{\maxr}{7}
\pgfmathsetmacro{\p}{2.3}
\pgfmathsetmacro{\e}{1.2}
\pgfmathsetmacro{\a}{\p / (1 - \e^2)}
\pgfmathsetmacro{\b}{\p / sqrt(\e^2 - 1)}
%\pgfmathsetmacro{\c}{sqrt(\a^2 - \b^2)} % XXX: ellipse only!
\pgfmathsetmacro{\c}{-\a*\e}
\pgfmathsetmacro{\q}{\p / (1 + \e)}
\pgfmathsetmacro{\directrixx}{\p / \e}
\pgfmathsetmacro{\maxf}{rad(acos((\p / \maxr - 1) / \e))}
\pgfmathsetmacro{\f}{110} % true anomaly in degrees
\pgfmathsetmacro{\tanhF}{sqrt(\e^2 - 1) * sin(\f) / (\e + cos(\f))} % tanh(F)
% note: atanh(x) = ln((1+x)/(1-x))/2
\pgfmathsetmacro{\F}{ln((1+\tanhF)/(1-\tanhF))/2}
\pgfmathsetmacro{\Pr}{\p / (1 + \e * cos(\f))}
\pgfmathsetmacro{\Px}{\Pr * cos(\f)}
\pgfmathsetmacro{\Py}{\Pr * sin(\f)}
\pgfmathsetmacro{\Qx}{\a*(cosh(\F) - \e)}
\pgfmathsetmacro{\Qy}{-\a * sinh(\F)}
\coordinate (F) at (0,0);
\coordinate (C) at (\c,0);
\coordinate (Pe) at (\q,0);
\coordinate (Mi) at (-\c,-\b);
\coordinate (Re) at (0,-\p);
\coordinate (P) at (\Px, \Py);
\coordinate (Q) at (\Qx, \Qy);
\coordinate (N) at (\directrixx, \Py);
% grid and axes
%\draw[gray,thin] (-\maxr,-\maxr) grid (\maxr,\maxr);
\draw[->, thin] (-\maxr, 0) -- (\maxr+0.2, 0)
node[right]{$x$};
\draw[->, thin] (0, -\maxr) -- (0, \maxr+0.2)
node[above]{$y$};
% conic section
\draw[thick, domain=-\maxf:\maxf, samples=200, smooth]
plot (xy polar cs:angle=\x r, radius={\p / (1 + \e * cos(\x r))});
% arrow to point on conic
\draw[->, thick] (F) -- (P)
node[midway, anchor=east]{$r$}
node[anchor=south west]{$P$};
\draw[->, thick] (C) -- (Q)
node[anchor=south west]{$Q$};
\draw[thin, dashed] (C) -- ({\a * (cosh(\F) - \e)}, {\b * sinh(\F)});
% directrix
\draw[thick] (\directrixx,-\maxr) -- (\directrixx,\maxr)
node[above, color=black]{Directrix};
\draw[thick] (\c,-\maxr) -- (\c,\maxr);
%node[above, color=black]{Center};
%\draw[thick] (2*\c-\directrixx,-\maxr) -- (2*\c-\directrixx,\maxr)
%node[above, color=black]{Directrix};
% asymptotes
\draw[thin, dashed]
({\maxr * \a / \b + \c}, \maxr) --
(\maxr, {(\b / \a) * (\maxr - \c)});
\draw[thin, dashed]
({\maxr * \a / \b + \c}, -\maxr) --
(\maxr, {-(\b / \a) * (\maxr - \c)});
% major and minor axes
\draw[thin, dashed]
(\c, \b) -- (\q, \b) -- (\q, -\b) -- (\c, -\b);
\draw[thin, dashed] (C) ++(0,\c) arc [radius={\c}, start angle=90, end angle=270];
% auxiliary hyperbola with b = a (concentric)
\draw[thin, smooth, domain=-\maxr:\maxr]
plot ({\c - sqrt((\a)^2 * (1 + (\x)^2 / (\a)^2))}, {\x});
% fill area swept by hyperbolic anomaly
\fill[color=gray, opacity=0.5, domain=0:\F, smooth]
(C) --
plot ({\c + \a * cosh(\x)}, {-\a * sinh(\x)});
% fill area swept by satellite
\fill[color=gray, opacity=0.5, domain=0:rad(\f), samples=100, smooth]
(F) --
plot (xy polar cs:angle=\x r, radius={\p / (1 + \e * cos(\x r))});
% arcs for true and hyperbolic anomaly
\draw[->, thick]
(F) node[anchor=south west]{$f$}
++(0.7,0) arc [start angle=0, end angle={\f}, radius=0.7];
\draw[thick]
(C) node[anchor=north east]{$\frac{F}{2}$}
++(-0.7,0) arc [start angle=180, end angle={180-deg(\F / 2)}, radius=0.7];
% XXX: is the damn auxiliary hyperbola concentric or confocal???
% XXX: confocal auxiliary parabola
\pgfmathsetmacro{\paux}{\q * (1 + sqrt(2))}
\pgfmathsetmacro{\eaux}{sqrt(2)}
\pgfmathsetmacro{\maxfaux}{rad(acos((\paux / \maxr - 1) / \eaux))}
\pgfmathsetmacro{\aaux}{\paux / (1 - \eaux^2)}
\pgfmathsetmacro{\baux}{\paux / sqrt(\eaux^2 - 1)}
\pgfmathsetmacro{\caux}{-\aaux*\eaux}
\coordinate (Caux) at (\caux,0);
%\draw[thin, domain=-\maxfaux:\maxfaux, smooth]
%plot (xy polar cs:angle=\x r, radius={\paux / (1 + \eaux * cos(\x r))});
%\draw[thin, dashed]
%({\Px}, 0) -- ({\Px}, {\maxr});
% \draw[thin] ({\caux}, 0) -- (P);
%\draw[->, thick]
% (Caux) node[anchor=north east]{$\frac{F}{2}$}
% ++(-0.7,0) arc [start angle=180, end angle={180-deg(\F / 2)}, radius=0.7];
\end{tikzpicture}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment