[Tex/LaTex] Improved Unit Circle Animation

animationspgfplotstikz-pgf

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]
        \addplot[domain=0:\theangle,no markers,cyan] {2.5*sin(x)};
        \addplot[domain=0:\theangle,no markers,orange] {2.5*cos(x)};
    \end{axis}
    \end{scope}
    \end{tikzpicture}
%
    \stepcounter{angle}
    \ifthenelse{\theangle<359}{
        \newframe
    }{
        \end{animateinline}
    }
}
\end{document}

Best Answer

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);
        \addplot[domain=0:\theangle,ultra thick, no markers,orange] {cos(x)};
    \end{axis}
    \draw [densely dashed,cyan, thick] (B) -- (C);
    \end{scope}
    \end{tikzpicture}
%
    \stepcounter{angle}
    \ifthenelse{\theangle<359}{
        \newframe
    }{
    }
}
 \end{animateinline}
\end{document}