16

I am trying to fill in space between a pair of spiral curves in tikz. I have tried using the fillbetween library but it just shades in the whole spiral. Does anyone have a hint they could offer? Thanks.

enter image description here

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usepgfplotslibrary{polar}  
\begin{document}
\begin{tikzpicture}[scale=0.5]
   \begin{polaraxis}[hide axis]
    \addplot [mark=none,domain=0:720,samples=600,thick,gray] {0.5*x};
    \addplot [mark=none,domain=0:720-180,samples=600,thick,gray] {-0.667*x};    

    %\addplot [mark=none,domain=0:720,samples=600,thick,gray] {-0.5*x};     
    %\addplot [mark=none,domain=0:720-180,samples=600,thick,gray] {0.667*x}; 

    \end{polaraxis}
\end{tikzpicture}
\end{document}
  • 3
    Welcome to TeX SE! Looks like a good first question, too. It has a complete but minimal example which always makes people happy ;). – cfr Apr 26 '15 at 20:19
  • I feel like the named paths/fill between answer in this question ought to hold the key, but I just can’t get it to work: tex.stackexchange.com/a/172883/9668 :-/ – alexwlchan Apr 26 '15 at 21:48
  • I have to say, you can come up with some really interesting filling patterns if you play with this. Too bad none of them are the right one :(. – cfr Apr 26 '15 at 23:36
6

In the meantime, here is a solution with MetaPost. Since the syntax of Tikz and MetaPost have many similarities, it may help to provide the desired solution.

The key point is to append the two spirals (one of them reverted), close the resulting path (--cycle instruction) and then fill it:

fill spiral1--reverse spiral2--cycle withcolor .8white;

I have inserted the MetaPost code into a LuaLaTeX program for typesetting convenience:

\documentclass[border=2mm]{standalone}
\usepackage{luamplib}
  \mplibsetformat{metafun}
\begin{document}
  \begin{mplibcode}
    vardef polarfcn(expr tmin, tmax, tstep)(text f_t) =
      save t; t := tmin;
      (f_t*cos t, f_t*sin t)
      forever: hide(t := t + tstep) exitunless t <= tmax;
        .. (f_t*cos t, f_t*sin t)
      endfor
      if t - tstep < tmax: hide(t := tmax) .. (f_t*cos t, f_t*sin t) fi
    enddef;
    u := cm;
    beginfig(1);
      path spiral[]; 
      spiral1 = polarfcn(0, 4pi, 4pi/600)(.5t) scaled u;
      spiral2 = polarfcn(0, 3pi, 3pi/600)(-2/3t) scaled u;
      fill spiral1--reverse spiral2--cycle withcolor .8white;
      draw spiral1; draw spiral2;
    endfig; 
  \end{mplibcode}
\end{document}

Output:

enter image description here

Edit Here is a variant (with the same output), inspired by Thruston's remark.

\documentclass[border=2mm]{standalone}
\usepackage{luamplib}
\begin{document}
  \begin{mplibcode}
    vardef polarpnt(expr r, t) = r*dir t enddef;
    vardef polarfcn(expr tmin, tmax, tstep)(text r) =
      save t; t := tmin;
      polarpnt(r, t)
      forever: hide(t := t + tstep) exitif t > tmax; .. polarpnt(r, t) endfor
      if t - tstep < tmax: hide(t := tmax) .. polarpnt(r, t) fi
    enddef;
    beginfig(1);
      path spiral[]; 
      spiral1 = polarfcn(0, 720, 1)(.5t);
      spiral2 = polarfcn(0, 540, 1)(-2/3t);
      fill spiral1 .. reverse spiral2 .. cycle withcolor .8white;
      draw spiral1; draw spiral2;
    endfig; 
  \end{mplibcode}
\end{document}
  • 1
    You could make the spirals rather more simply with something like: spiral1 = (0,0) for t = 0 step 3 until 720: -- ( 1/2t,0) rotated t endfor; – Thruston Apr 26 '15 at 21:08
  • Also, it probably would look slightly neater if you splice the spiral path segments with .. rather than --. – Thruston Apr 26 '15 at 21:10
  • @Thruston As for your second point, since the two spirals theoretically share their extremities, it makes (or it seems to make) no difference. As for the first point, you're quite right I'll post your alternative coding. – Franck Pastor Apr 26 '15 at 21:13
  • 1
    The path splicing point was just that .. is simpler (because -- is defined to be {curl 1}..{curl 1}). But you are right, for such a short connection it probably makes no difference. – Thruston Apr 26 '15 at 21:29
  • @Thruston I've posted the alternative. you may think I've complicated your suggestion, but I still wanted a general macro drawing any polar function, not necessarily starting at angle 0, but at any angle tmin, and necessarily stopping at tmax exactly — not before it, whatever the value of tstep. Also, I've used radians, because using degrees here would have required a big scaling. – Franck Pastor Apr 26 '15 at 21:51
6

I did manage to get the tikz code working. Below is the code. Thanks for the hints everyone.

\documentclass[tikz,border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usepgfplotslibrary{fillbetween}
\usepgfplotslibrary{polar}
\begin{document}
\begin{tikzpicture}[scale=0.5]
   \begin{polaraxis}[hide axis]
   \begin{scope}[]
        \addplot+[mark=none,domain=0:720,samples=100,line width=1pt,gray,name path=B] {0.5*x};
    \addplot+ [mark=none,domain=720-180:0,samples=100,thick,gray,name path=A] {-0.667*x};   
    \tikzfillbetween[of=A and B,on layer=,split,every even segment/.style={fill=none,draw=gray}]{gray}  
  \end{scope}
    \end{polaraxis}
  \end{tikzpicture}
\end{document}

swirl

  • Your solution doesn't work at me (I copy/past it in my WinEdt editor). I receive error: ! Missing number, treated as zero. <to be read again> \advance \end{polaraxis}. The error is the same if I use solution shown pp. 371 in pgfplots manual adopted to your polar coordinate system. – Zarko Apr 28 '15 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.