# Improved Unit Circle Animation

I'm trying to improve the sine/cosine animation provided on texample.net to suite my needs. I wanted to add a plot of the sine and cosine part and tried using pgfplots for this, as it can evaluate and draw functions without gnuplot. My Problem is the correct alignment of the plot against the circle and the drawing of the guiding line. I already found the sine and cosine functions included in tikz, but they only draw a small segment of the functions respectively. Maybe someone has an idea how to improve this.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{ifthen}
\usepackage[xetex]{animate}
\usetikzlibrary{calc}
\begin{document}
\newcounter{angle}
\setcounter{angle}{1}
\begin{animateinline}[loop, poster = first, controls]{30}

\whiledo{\theangle<359}{
%
\begin{tikzpicture}
% Axis
\draw[thick,->,black] (-3,0)--(4,0) node[below] {$x$}; % x axis
\draw[thick,->,black] (0,-3)--(0,3) node[left] {$y$}; % y axis
\draw[black,thin] (0,0) circle (2.5cm);
\node[black,below] at (2.7,0) {1};
\node[black,above] at (0.2,-3.2) {1};
%
\draw[ultra thick,orange] (0,0) -- (\theangle:2.5cm |- 0,0) node[midway,below] {$\cos \alpha$}; % UpOn y axis
%
\draw (1,0) arc (0:\theangle:1) node at ($(\theangle/2:0.7)$) {$\alpha$};
\draw[ultra thick, cyan  ] (\theangle:2.5cm) -- (\theangle:2.5cm |- 0,0) node[midway,right] {$\sin \alpha$}; % vertical line
\draw[densely dotted,cyan] (\theangle:2.5cm) -- ($(4.5+\theangle/360*2*pi,0 |- \theangle:2.5cm)$); % horizontal line
\draw[ultra thick,red,->,rotate=\theangle] (0,0) -- (2.5,0);
\begin{scope}[xshift=4.5cm,yshift=-2.85cm]
\begin{axis}[axis y line=center, axis x line=middle, xmin=0, xmax=360, ymin=-3, ymax=3]
\end{axis}
\end{scope}
\end{tikzpicture}
%
\stepcounter{angle}
\ifthenelse{\theangle<359}{
\newframe
}{
\end{animateinline}
}
}
\end{document}

-

To align the axis correctly, you can first define a new coordinate at the tip of your left x axis using coordinate (<name>) and in your axis environment set anchor=origin, at=(<name>). To get the vertical size right, you can define the length of a y unit to be equal to the radius of your unit circle using y=2.5cm.

For the connecting line, I would also place coordinates at the end of the red arrow and at the end of the \addplot command:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{ifthen}
\usepackage[xetex]{animate}
\usetikzlibrary{calc}
\begin{document}
\newcounter{angle}
\setcounter{angle}{1}
\begin{animateinline}[loop, poster = first, controls]{30}

\whiledo{\theangle<359}{
%
\begin{tikzpicture}
% Axis
\draw[thick,-stealth,black] (-3,0)--(4,0) coordinate (A) node[below] {$x$}; % x axis
\draw[thick,-stealth,black] (0,-3)--(0,3) node[left] {$y$}; % y axis
\draw[black,thin] (0,0) circle (2.5cm);
\node[black,below] at (2.7,0) {1};
\node[black,above] at (0.2,-3.2) {1};
%
\draw[ultra thick,orange] (0,0) -- (\theangle:2.5cm |- 0,0) node[midway,below] {$\cos \alpha$}; % UpOn y axis
%
\draw (1,0) arc (0:\theangle:1) node at ($(\theangle/2:0.7)$) {$\alpha$};
\draw[ultra thick, cyan  ] (\theangle:2.5cm) -- (\theangle:2.5cm |- 0,0) node[midway,right] {$\sin \alpha$}; % vertical line

\draw[ultra thick,red,-stealth,rotate=\theangle] (0,0) -- (2.5,0) coordinate (B);

\begin{scope}
\begin{axis}[
thick,
y=2.5cm,
axis lines=center,
xmin=0, xmax=360,
ymin=-1, ymax=1,
anchor=origin, at=(A),
xshift=3ex,
enlarge y limits,
enlarge x limits=upper,
samples=90,
xtick={0,90,...,360}]
\addplot[domain=0:\theangle,ultra thick, no markers,cyan] {sin(x)} coordinate (C);
\end{axis}
\draw [densely dashed,cyan, thick] (B) -- (C);
\end{scope}
\end{tikzpicture}
%
\stepcounter{angle}
\ifthenelse{\theangle<359}{
\newframe
}{
}
}
\end{animateinline}
\end{document}

-

You can place nodes/coordinates on the pgfplots curves and use them later for referencing.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
%\usepackage{ifthen}
\usepackage[xetex]{animate}
\usetikzlibrary{calc}
\begin{document}
\newcounter{angle}
\setcounter{angle}{1}
\begin{animateinline}[loop, poster = first, controls]{30}

\whiledo{\theangle<359}{
%
\begin{tikzpicture}
% Axis
\draw[thick,->,black] (-3,0)--(4,0) node[below] {$x$}; % x axis
\draw[thick,->,black] (0,-3)--(0,3) node[left] {$y$}; % y axis
\draw[black,thin] (0,0) circle (2.5cm);
\node[black,below] at (2.7,0) {1};
\node[black,above] at (0.2,-3.2) {1};
%
\draw[ultra thick,orange] (0,0) -- (\theangle:2.5cm |- 0,0) node[midway,below] {$\cos \alpha$}; % UpOn y axis
%
\draw (1,0) arc (0:\theangle:1) node at ($(\theangle/2:0.7)$) {$\alpha$};
\draw[ultra thick, cyan  ] (\theangle:2.5cm) -- (\theangle:2.5cm |- 0,0) node[midway,right] {$\sin \alpha$}; % vertical line
\draw[ultra thick,red,->,rotate=\theangle] (0,0) -- (2.5,0);
\begin{scope}[xshift=4.5cm,yshift=-2.85cm]
\begin{axis}[axis y line=center, axis x line=middle, xmin=0, xmax=360, ymin=-3, ymax=3]
\end{axis}
\end{scope}
\draw[densely dotted,cyan] (\theangle:2.5cm) -- (c); % Change to (s) for sine curve tracking
\end{tikzpicture}
%
\stepcounter{angle}
\ifnum\theangle<359\newframe\else\end{animateinline}\fi
}
\end{document}


Here is a snapshot with cosine tracking. For the scaling please have a look at (axis cs:x,y) coordinate system to get a reference from the axis while drawing outside of the axis. Hence, it's better to draw the axis and get the useful info out of it via axis cs.

-

With PSTricks:

\documentclass[pstricks,border=0pt]{standalone}
\usepackage{pst-plot,pst-node}
\usepackage[nomessages]{fp}

% User Defined Constants
\FPeval\NumberOfPoints{18}

% Internally Defined Data
\FPeval\DeltaX{round(2*pi/(NumberOfPoints-1):3)}

\def\sine#1{%
\begin{pspicture}(-0.25,-1.25)(\dimexpr\psPiTwo\psxunit+0.6\psxunit\relax,1.60)
\psplot[algebraic,linecolor=blue]{0}{#1}{sin(x)}
\psaxes
[
linecolor=gray,
ticksize=2pt -2pt,
labelFontSize=\scriptscriptstyle,
trigLabels=true,trigLabelBase=2,dx=\psPiH
]{->}(0,0)(-0.10,-1.10)(\dimexpr\psPiTwo\psxunit+0.3\psxunit\relax,1.30)[$x$,0][$y$,90]
\end{pspicture}}

\def\circle#1{%
\begin{pspicture}(-1.25,-1.25)(1.6,1.6)
\pscircle[linecolor=gray](0,0){1}
\pnode(!#1 RadtoDeg dup cos exch sin){A}
\psline{->}(A)
\psaxes
[
linecolor=gray,
ticksize=2pt -2pt,
labelFontSize=\scriptscriptstyle,
]{->}(0,0)(-1.1,-1.1)(1.3,1.3)[$x$,0][$y$,90]
\end{pspicture}}

\begin{document}
\multido{\nx=0.000+\DeltaX}{\NumberOfPoints}
{
\begin{pspicture}[showgrid=false](0,-1.6)(10.2,1.6)
\rput(1.375,0){\circle\nx}
\rput(6.515,0){\sine\nx}
\pcline[linestyle=dashed,linecolor=red](A)(B)
\end{pspicture}
}
\end{document}

-
The complete solution for conforming to the questioner's requirement will be added as my next edit. – kiss my armpit Sep 4 '12 at 14:13