有没有一种简单的方法可以在 tikz 中对矩形进行着色,使颜色不是从一个变为另一个,而是在两者之间变化几次。类似于灰度值随位置 x 变化,如 cos(x)。
采用线性变化而不是正弦变化就可以了:我只是希望它能够模仿干涉图案。
答案1
你可以尝试阴影:
\documentclass[tikz,border=5]{standalone}
\begin{document}
\tikz[x=0.125cm,y=0.125cm]
\foreach \i in {0,1,...,21}
\path [left color=black, right color=white, shading angle={mod(\i,20)*180+90}]
(\i*.9,0) rectangle ++(1,10);
\end{document}
或褪色:
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{fadings}
\begin{tikzfadingfrompicture}[name=interference]
\foreach \i in {0,1,...,21}
\path [left color=transparent!0, right color=transparent!100, shading angle={mod(\i,2)*180+90}]
(\i*.9,0) rectangle ++(1,20);
\end{tikzfadingfrompicture}
\begin{document}
\foreach \i in {0,...,24}{
\begin{tikzpicture}
\clip (-5,-5) rectangle ++(10,10);
\path [fill=black, path fading=interference, fit fading=false, fading transform={rotate=\i*7.2}] (-10,-5]) rectangle ++(20,10);
\path [fill=black, path fading=interference, fit fading=false] (-10,-5) rectangle ++(20,10);
\end{tikzpicture}
}
\end{document}
在这两种情况下,使用\i*.9
都是为了解决相邻阴影之间出现的一些恼人的白线,这些白线可能是(或不是)观看者的伪影。
改变干涉图样的相位对于阴影来说比较棘手,但对于衰落来说则相当容易,如果衰落的范围大于所需路径。然后fading transform
可以使用键来移动“透过”路径看到的衰落。
在以下示例中,红色矩形说明了相对于褪色区域的褪色位置。此外,绘制了一条线来填充相邻阴影之间的白线,而不是上面描述的解决方法来消除相邻阴影之间出现的白线。
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{fadings}
\begin{tikzfadingfrompicture}[name=interference]
\foreach \i in {0,1,...,19}{
\path [left color=transparent!0, right color=transparent!100, shading angle={mod(\i,2)*180+90}]
(\i,0) rectangle ++(1,10);
\ifodd\i\else% Fill in gap
\path [draw=transparent!0] (\i,0) -- ++(0,10);
\fi
}
\end{tikzfadingfrompicture}
\begin{document}
\foreach \i in {-10,...,10,9,8,...,-9}{%
\begin{tikzpicture}
\path [draw=red, shift=(0:\i/2)] (-10,-5) rectangle (10,5);
\path [fill=black, path fading=interference, fit fading=false, fading transform={shift=(0:\i/2)}] (-5,-5) rectangle (5,5);
\useasboundingbox (-15,-5) rectangle (15,5);
\end{tikzpicture}%
}
\end{document}
使用淡入淡出意味着你也可以做这样的很酷的事情:
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{fadings}
\begin{tikzfadingfrompicture}[name=interference]
\foreach \i in {1,...,15}
\foreach \j in {1,...,25}
\path [line width=\j, draw=transparent!0,opacity=1/30]
(0:\i) arc (0:180:\i);
\end{tikzfadingfrompicture}
\begin{document}
\begin{tikzpicture}
\clip (-5,-5) rectangle ++(10,10);
\path [fill=black, path fading=interference, fit fading=false] (-10,-5) rectangle ++(20,10);
\path [fill=black, path fading=interference, fit fading=false, fading transform={rotate=45}] (-10,-5) rectangle ++(20,10);
\end{tikzpicture}
\end{document}
答案2
您可以通过填充大量细矩形来绘制自己的阴影图案,每个矩形都通过 sin() 函数计算出纯灰色:
\usetikzlibrary{calc}
\begin{tikzpicture}
\def\detail{300}
\pgfmathsetmacro{\width}{8/\detail}
\foreach \i in {0,...,\detail} {
\pgfmathsetmacro{\shade}{45+45*sin(\i*10)}
\fill[fill=black!\shade!white] (\i*\width,0) rectangle +(\width, 2);
}
\end{tikzpicture}
更新
针对评论中史蒂夫的问题,这里有一个新版本的代码,更加可定制,并且具有更好的变量名:
\usetikzlibrary{calc}
\begin{tikzpicture}
% ---- Customizable section
\def\numsamples{200} % The greater, the thinner each sample
\def\numcycles{8} % Number of "white bars" in the result
\def\totalwidth{10} % Width (in cm) of the resulting box
% ---- End of customizable section
\pgfmathsetmacro{\samplewidth}{\totalwidth/\numsamples}
\pgfmathsetmacro{\frequency}{360/\numsamples*\numcycles}
\foreach \i in {0,...,\numsamples} {
\pgfmathsetmacro{\shade}{50+50*sin(\i*\frequency)}
\fill[draw=none, fill=black!\shade!white] (\i*\samplewidth,0) rectangle +(\samplewidth*1.1, 2);
}
\end{tikzpicture}
上述代码产生以下“低分辨率”结果:
增加到\numsamples
500,您会获得更好的结果:
答案3
实际上,使用 pgfplots 和 gnuplot 的表面图更加灵活。
这里是 7 个点源的干涉图样示例,其参数如下: * \Lambda 是波长 * \DistanceSources 是源之间的恒定距离 * 振幅可以用函数“amorti”选择,但在距源 \Cutoff 的半径内是恒定的 * \RetardIIvsI 是两个相邻源之间的恒定相移 * \Date 是添加到所有源的恒定相位
\documentclass{standalone}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{tikzpicture}[]
\pgfmathsetmacro{\xmin}{-}6
\pgfmathsetmacro{\ymin}{-6}
\pgfmathsetmacro{\xmax}{6}
\pgfmathsetmacro{\ymax}{6}
\pgfmathsetmacro{\Lambda}{.5}
\pgfmathsetmacro{\DistanceSources}{\Lambda/2}
\pgfmathsetmacro{\CentreI}{-4*\DistanceSources}
\pgfmathsetmacro{\CentreII}{\CentreI+\DistanceSources}
\pgfmathsetmacro{\CentreIII}{\CentreII+\DistanceSources}
\pgfmathsetmacro{\CentreIV}{\CentreIII+\DistanceSources}
\pgfmathsetmacro{\CentreV}{\CentreIV+\DistanceSources}
\pgfmathsetmacro{\CentreVI}{\CentreV+\DistanceSources}
\pgfmathsetmacro{\CentreVII}{\CentreVI+\DistanceSources}
\pgfmathsetmacro{\Cutoff}{\Lambda/10}
\pgfmathsetmacro{\RetardIIvsI}{pi/2}
\pgfmathsetmacro{\Date}{0}
\begin{axis}[colormap/blackwhite,
view ={0}{90},
xlabel = $x$,
ylabel = $y$,
extra x ticks = ,
extra x tick labels = ,
extra y ticks = ,
extra y tick labels = ,
xmin = \xmin,
ymin = \ymin,
xmax = \xmax,
ymax = \ymax,
domain = \xmin:\xmax,
samples = 50
]
\addplot3[surf,shader=interp,raw gnuplot] gnuplot {
set isosamples 50,50;
amorti (centre,xy,y,cutoff)= 1;
interf(centre,date,x,y,cutoff,lambda,retard) =%
amorti(centre,x,y,cutoff)%
* cos(date-2*pi*sqrt((x-centre)**2 + y**2)/lambda-retard);
splot [x=\xmin:\xmax] [y=\ymin:\ymax] 1./7.*(%
interf(\CentreI,\Date,x,y,\Cutoff,\Lambda,0)%
+ interf(\CentreII,\Date,x,y,\Cutoff,\Lambda,\RetardIIvsI)%
+ interf(\CentreIII,\Date,x,y,\Cutoff,\Lambda,2*\RetardIIvsI)%
+ interf(\CentreIV,\Date,x,y,\Cutoff,\Lambda,3*\RetardIIvsI)%
+ interf(\CentreV,\Date,x,y,\Cutoff,\Lambda,4*\RetardIIvsI)%
+ interf(\CentreVI,\Date,x,y,\Cutoff,\Lambda,5*\RetardIIvsI)%
+ interf(\CentreVII,\Date,x,y,\Cutoff,\Lambda,6*\RetardIIvsI)%
)
};
\end{axis}
\end{tikzpicture}
\end{document}