TransWikia.com

Shading with polar curves when using tikzfillbetween

TeX - LaTeX Asked by Yacola on January 8, 2021

I am currently trying to plot a portion of a ring which got distorted at one point. To this end, I am plotting two curves into polar coordinates with tikzfillbetween these two paths.

Here is the minimal working example:

documentclass[tikz,border=2mm]{standalone}

% packages
usepackage{tikz}
usepackage{pgfplots}
pgfplotsset{compat=newest}
usepgfplotslibrary{fillbetween}

% parameters
defclr{7}       % clearance
defpen{-6.}     % negative penetration
deffz{0.625}    % fake zero
defnl{2}        % number of lobes
defwl{0.18}     % width of lobes
defth{0.3}      % thickness

begin{document}
begin{tikzpicture}
    
    % bottom path A
    draw [line width=1.4pt,,color=black!50,domain=0.3*pi:0.7*pi,samples=256,smooth, name path=A] plot (xy polar cs:angle=x r,radius= {clr-(clr+pen)*exp(-((x - (pi/nl))/wl)^2))});
    % top path B
    draw [thick,color=black!50,domain=0.3*pi:0.7*pi,samples=256,smooth, name path=B] plot (xy polar cs:angle=x r,radius= {th + clr-(clr+pen)*exp(-((x - (pi/nl))/wl)^2))});
    
    tikzfillbetween[of=A and B]{color=gray!50};
    
end{tikzpicture}
end{document}

And the current output:

ring

Question: How one can add some kind of shading between these curves (that is not really radial) so that it fades away when it goes from inner path to outer path gradually?

Expected output should look like below but curved like the above picture:

draw[line width=1.4pt,color=black!50,name path=A] (0,0) -- (12,0);
draw[thick,color=black!50,name path=B] (0,0.5) -- (12,0.5);
tikzfillbetween[of=A and B]{bottom color = gray!50, top color= gray!5};

output

Any help would be greatly appreciated.

2 Answers

One way you can do this is to use a 3D plot for the shading. We set the domain of the radius to go from 0 to your thickness (th). Then plot your curve across this radius while increasing the z coordinate as we go.

This produces a conical kind of shape. Then we change the view to look down directly from above (view={0}{90}) so we get a 2D plot showing the shading. The shading is set as you want using a custom colormap running from black!50 to black!5.

Once the 3D plot is drawn you can use your existing draw commands to draw the inner and outer curves over the top. These commands must be placed within the axis environment so that the same coordinate system is used.

documentclass[border=5mm]{standalone}
usepackage{pgfplots}
pgfplotsset{compat=newest}
usepgfplotslibrary{polar}

% parameters
defclr{7}       % clearance
defpen{-6.}     % negative penetration
deffz{0.625}    % fake zero
defnl{2}        % number of lobes
defwl{0.18}     % width of lobes
defth{0.3}      % thickness

pgfplotsset{
  colormap={greycolormap}{color=(black!50) color=(black!5)}
}

begin{document}
begin{tikzpicture}
  begin{axis}[
      view={0}{90},
      data cs=polarrad,
      domain=0.3*pi:0.7*pi,
      y domain=0:th,
      samples=32,
      axis equal,
      hide axis,
      colormap name=greycolormap,
      declare function={
        innerring(x)=clr-(clr+pen)*exp(-((x-(pi/nl))/wl)^2);
        outerring(x)=th+innerring(x);
      }
    ]
    addplot3 [surf, shader=interp]
      (x,
       {y + innerring(x)},
       y);
    draw [line width=1.4pt, black!50, domain=0.3*pi:0.7*pi, samples=32, smooth]
      plot (xy polar cs:angle=x r, radius={innerring(x)});
    draw [thick, black!50, domain=0.3*pi:0.7*pi, samples=32, smooth]
      plot (xy polar cs:angle=x r, radius={outerring(x)});
  end{axis}
end{tikzpicture}
end{document}

output

Correct answer by David Purton on January 8, 2021

I do not know, whether fillbetween can do that, but you can finetune sth. like that:

% Filling
foreach a in {0,0.01,...,0.3}{
pgfmathsetmacroc{35-100*a}
draw[Curve, red!c] plot (xy polar cs:angle=x r, radius= {a+R1(x)});
}

enter image description here

documentclass[tikz,border=2mm]{standalone}

% packages
usepackage{tikz}
usepackage{pgfplots}
pgfplotsset{compat=newest}
usepgfplotslibrary{fillbetween}

% parameters
defclr{7}       % clearance
defpen{-6.}     % negative penetration
deffz{0.625}    % fake zero
defnl{2}        % number of lobes
defwl{0.18}     % width of lobes
defth{0.3}      % thickness

begin{document}
begin{tikzpicture}[
declare function={
R1(x)=clr-(clr+pen)*exp(-((x - (pi/nl))/wl)^2));
R2(x)=th + clr-(clr+pen)*exp(-((x - (pi/nl))/wl)^2));
},
Curve/.style={
line width=1.4pt, color=black!50, domain=0.3*pi:0.7*pi, samples=256, smooth,
},
]
% Filling
foreach[count=n] a in {0,0.01,...,0.3}{
pgfmathsetmacroc{35-100*a}
draw[Curve, red!c] plot (xy polar cs:angle=x r, radius= {a+R1(x)});
%node[yshift=-n cm] {c};
}

% inner path A
draw[Curve, name path=A] plot (xy polar cs:angle=x r,radius= {R1(x)});
% outer path B
draw[Curve, name path=B] plot (xy polar cs:angle=x r,radius= {R2(x)});

%tikzfillbetween[of=A and B]{bottom color=gray!66, top color=gray!33};
end{tikzpicture}
end{document}

Answered by cis on January 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP