# How to draw a sine wave on a circular path in tikz

I want to draw circular waves like these:

in TikZ.

-
Welcome to TeX.sx! On this site, a question should typically revolve around an abstract issue (e.g. "How do I get a double horizontal line in a table?") rather than a concrete application (e.g. "How do I make this table?"). Questions that look like "Please do this complicated thing for me" tend to get closed because they are "too localized". Please try to make your question clear and simple by giving a minimal working example (MWE): you'll stand a greater chance of getting help. –  hpesoj626 Nov 14 '12 at 23:30
Well to start off with, it would be helpful if you figured out the equation(s) to that represents that path you want to draw -- that is a math issue, not a TeX issue. Once you get that (perhaps in parametrized form), the I'd suggest you use pgfplots pacakge to draw it. There should be plenty of examples on this site for that. Once you make an attempt and run into a specific issue, then perhaps you can update this question showing what you attempted, and then people here could help you with a specific issue. –  Peter Grill Nov 14 '12 at 23:38

I'll draw the first one and you try to work on the rest. You can do this iteratively, too, I think, with some extra work.

\documentclass[tikz,border=10]{standalone}

\begin{document}

\begin{tikzpicture}[scale=3,very thick]
\draw[color=red,domain=0:6.28,samples=200,smooth] plot (canvas polar
\draw[,dashed,domain=0:6.28,samples=200,smooth] plot (canvas polar
\end{tikzpicture}

\end{document}


As have been said in the comment above, once you have the mathematical equation in polar coordinates, it is just a matter of some pgfplots work. To learn more about pgplots, which is built upon tikz, type texdoc pgfplots in your terminal.

## Edit

This is not a pgfplots solution BTW. As have been noted by Paul Gaborit in comment, pgfplots is superfluous in the given code. I was about to go back to it since I mentioned pgfplots in my answer. I've been away for a while. Anyway, others have already provided other solutions so I will leave my answer as it is.

-
Thank you that was exactly what I needed. –  user22117 Nov 15 '12 at 0:07
\usepackage{pgfplots} is superfluous... –  Paul Gaborit Nov 15 '12 at 0:33
@PaulGaborit Yeah. Removed it. I was working with article class before replacing it with standalone. And I was thinking of pgfplots solution earlier. Thanks for the heads up. –  hpesoj626 Nov 15 '12 at 8:52
Adding border=10 to the options to the standalone class can help when the clipping is overzealous. –  Loop Space Nov 15 '12 at 9:56
@hpesoj626 if you do not mind, I will "copy" something inspired by your example into the pgfplots manual as an example for how to provide polar coordinates into a cartesian axis using data cs=polar (your example is prettier than mine in the manual) –  Christian Feuersänger Nov 17 '12 at 16:39

The hardest part is to parametrize the curve. There are lots of ways to do this, one such way is

x(t) = (2+.5*cos(nt))cos(t)
y(t) = (2+.5*cos(nt))sin(t)


where 0\leq t\leq 2\pi and n\in \{3, 4, 5, ....\}

If you vary the .5 it will vary the sharpness of the curve.

Once you have this, you can plot the curve easily using pgfplots and its \addplot command

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[axis equal,axis lines=none]
\end{axis}
\end{tikzpicture}

\end{document}


Animation: vary n

\documentclass[tikz]{standalone}

\usepackage{pgfplots}
\usepackage{amsmath}

\begin{document}

\foreach \n in{3,4,...,10}{%
\begin{tikzpicture}
\begin{axis}[axis equal,
xmin=-3,xmax=3,
ymin=-3,ymax=3,
axis lines=none]
\node at (axis cs:0,0){$n=\n$};
\end{axis}
\end{tikzpicture}
}

\end{document}


Animation: vary 'sharpness' of the curve

\documentclass[tikz]{standalone}

\usepackage{pgfplots}
\usepackage{amsmath}

\begin{document}

\foreach \r in{0.1,0.2,...,1}{%
\begin{tikzpicture}
\begin{axis}[axis equal,
xmin=-3,xmax=3,
ymin=-3,ymax=3,
axis lines=none]
\node at (axis cs:0,0){$r=\r$};
\end{axis}
\end{tikzpicture}
}

\end{document}


See How to convert pstricks animation to GIF file? for full details of the rest of the animation creation process (just a couple of steps).

-
GarbageCollector has a rival, I see! –  Loop Space Nov 15 '12 at 9:43

Here is another TikZ solution...

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\def\r{1cm}
\def\v{2.5mm}
\foreach \n in {3,4,5,6}{
\begin{scope}[xshift=\n*2*(\r+\v+1mm)]
\draw[thick]  (0:{\r+\v})
\foreach \a in {1,...,359}{ -- (\a:{\r+cos(\a*\n)*\v}) } -- cycle;
\draw[dashed] circle (\r);
\end{scope}
}
\end{tikzpicture}
\end{document}

-

If you're not too bothered about the exact equation, here's a method using the hobby package (although you'd need the development version for it to work with foreach, it's nearly ready for upload to CTAN - just needs documentation - but for now it can be found at TeX-SX Launchpad, download hobby.dtx and run tex hobby.dtx).

\documentclass{article}
%\url{http://tex.stackexchange.com/q/82773/86}
\usepackage{tikz}
\usetikzlibrary{hobby}

\begin{document}
\begin{tikzpicture}
\draw[dashed]
\def\n{8}
\def\amp{1}
\draw[use Hobby shortcut] ([closed]3-\amp,0) \foreach \k in {1,...,\n}
{ .. (\k*360/\n:{3+(Mod(\k,2) == 1 ? \amp : -\amp)})};
\end{tikzpicture}
\end{document}


Two obvious parameters: \n is twice the number of "lumps" and \amp is the (half) amplitude.

I don't know how to do fancy animations, so here's a static image:

-

Just 4 fun with PSTricks. (and probably Bohr used PSTricks to illustrate his atomic model.)

\documentclass[pstricks]{standalone}
\usepackage{pst-plot}

\psset
{
unit=\psrunit,
polarplot,
algebraic=true,
plotpoints=150,
}

\begin{document}
\begin{pspicture}(-3,-3)(3,3)
\pscircle[linestyle=dashed](0,0){2}
\psplot[linecolor=red]{0}{TwoPi}{2+.5*sin(3*x)}
\end{pspicture}
\end{document}


## Animated version:

\documentclass[pstricks]{standalone}
\usepackage{pst-plot}

\psset
{
unit=\psrunit,
polarplot,
algebraic=true,
plotpoints=1000,
}

\begin{document}
\multido{\i=2+1}{25}{%
\begin{pspicture}(-3,-3)(3,3)
\pscircle[linestyle=dashed](0,0){2}
\psplot[linecolor=red]{0}{TwoPi}{2+.5*sin(\i*x)}
\end{pspicture}}
\end{document}


## Edit:

I am a bit lazy to redo my work above. Please try cos instead of sin if you want to shift the curve period/4 along the angular direction.

## The last edit:

The requested random magic by Doctor Kumar is given as follow. Please compile with pdflatex -shell-escape bohring.

% the filename of this code is bohring (not boring!)
\documentclass[preview,border=12pt]{standalone}
\usepackage{filecontents}
\def\filename{Bohr}

\begin{filecontents*}{\filename}
\documentclass[pstricks]{standalone}
\usepackage{pst-plot}

\psset
{
unit=\psrunit,
polarplot,
algebraic=true,
plotpoints=1000,
}

\begin{document}
\multido{\i=1+1}{25}{%
\begin{pspicture}(-3,-3)(3,3)
\pscircle[linestyle=dashed](0,0){2}
\psplot[linecolor=red]{0}{TwoPi}{2+.5*cos(\i*x)}
\end{pspicture}}
\end{document}
\end{filecontents*}

\usepackage{animate}

\immediate\write18{latex \filename}
\immediate\write18{dvips \filename}
\immediate\write18{ps2pdf \filename.ps}

% begin cleaning
% The following codes are written with Windows' shell commands only for Windows user.
% If you use Linux, then ask other people to translate the codes to Linux's equivalent.
% If you have no friend who can help you, just comment the code and manually remove the associated files.
\makeatletter
\@for\x:={tex,dvi,ps,log,aux}\do{\immediate\write18{cmd /c del \filename.\x}}
\makeatother
% end cleaning

\begin{document}
\animategraphics[controls,loop,autoplay,scale=1]{2}{\filename}{}{}
\end{document}

-
Why the code doesn't contain convert magic line ? ;-) –  Harish Kumar Nov 18 '12 at 0:40
+1 for Bohring :-) –  Harish Kumar Nov 18 '12 at 0:42
@HarishKumar: Can you remove the preview environment sandwiching the animategraphics as standalone does it by default? I am limited by the system so I can have 5 edits a day. –  Please don't touch Apr 25 '13 at 12:09
Done. Is it OK? –  Harish Kumar Apr 25 '13 at 22:44
@HarishKumar: Thank you very much. It suits my intent. –  Please don't touch Apr 26 '13 at 5:09