我目前正在尝试描绘一个环的一部分,该环在某一点处变形了。为此,我首先在极坐标中绘制两条曲线,然后tikzfillbetween
在这两条路径之间使用命令。
以下是最小的工作示例:
\documentclass[tikz,border=2mm]{standalone}
% packages
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
% parameters
\def\clr{7} % clearance
\def\pen{-6.} % negative penetration
\def\fz{0.625} % fake zero
\def\nl{2} % number of lobes
\def\wl{0.18} % width of lobes
\def\th{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}
当前输出:
问题:如何在这些曲线之间添加某种阴影(不是真正的径向),以便它在从内路径逐渐移动到外路径时逐渐消失?
预期输出应如下所示但像上图那样弯曲:
\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};
任何帮助将不胜感激。
答案1
一种方法是使用 3D 图进行着色。我们将半径的范围设置为从 0 到厚度 ( \th
)。然后在此半径上绘制曲线,同时增加坐标z
。
这会产生一种圆锥形。然后我们将视图更改为从上方直接向下看( ),这样我们就得到了一个显示阴影的 2D 图。使用从到的自定义运行,view={0}{90}
可以根据需要设置阴影。colormap
black!50
black!5
绘制完 3D 图后,您可以使用现有\draw
命令在顶部绘制内曲线和外曲线。这些命令必须放置在环境中,axis
以便使用相同的坐标系。
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{polar}
% parameters
\def\clr{7} % clearance
\def\pen{-6.} % negative penetration
\def\fz{0.625} % fake zero
\def\nl{2} % number of lobes
\def\wl{0.18} % width of lobes
\def\th{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}
答案2
我不知道是否fillbetween
可以做到这一点,但你可以像这样微调某事:
% Filling
\foreach \a in {0,0.01,...,0.3}{
\pgfmathsetmacro\c{35-100*\a}
\draw[Curve, red!\c] plot (xy polar cs:angle=\x r, radius= {\a+R1(\x)});
}
\documentclass[tikz,border=2mm]{standalone}
% packages
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
% parameters
\def\clr{7} % clearance
\def\pen{-6.} % negative penetration
\def\fz{0.625} % fake zero
\def\nl{2} % number of lobes
\def\wl{0.18} % width of lobes
\def\th{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}{
\pgfmathsetmacro\c{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}