我喜欢画一条双线,这样两条线之间的间隙是透明的。我的目的是画铁轨:
\documentclass[tikz]{standalone}
\tikzstyle{track}=[
postaction={draw=gray,densely dashed,line width=14pt},
postaction={draw=black,double distance=8pt,line width=2pt},
postaction={draw=gray,densely dashed,line width=8pt},]
\begin{document}
\begin{tikzpicture}
\draw[track] (-5,27) to[out= 0,in=180] ( 5,33)
to[out= 0,in= 90] ( 7,25)
to[out=270,in= 0] (-5,27);
\draw[track] (-4,10) to (0,10) to[out= 0,in=270] (4,16);
\draw[track] (12,10) to (8,10) to[out=180,in=270] (4,16) to (4,20);
\draw[track] (-4,0) to (12,0);
\draw[track] (0,0) to[out= 0,in=270] (3,2)
to[out= 90,in=270] (1,4)
to[out= 90,in=180] (4,6)
to[out= 0,in=120] (8,5)
to[out=300,in= 0] (4,0);
\end{tikzpicture}
\end{document}
第二个后动作绘制了一条双线,但两条线之间的间隙不透明。因此,我做了第三个后动作。但我希望如果双线之间的间隙是透明的,轨道看起来会更加逼真(特别是在道岔处)。有什么想法吗?
答案1
这可以通过使用淡入淡出和装饰来实现(当然需要付出一些努力)。我猜以下内容比其他内容更能证明概念,但可能会很有用:
\documentclass[tikz, border=5]{standalone}
\usetikzlibrary{decorations,fit,fadings}
% Layers
\pgfdeclarelayer{sleeper}
\pgfdeclarelayer{rail}
\pgfsetlayers{sleeper,rail,main}
\pgfdeclaredecoration{tracks}{final}{%
\state{final}{%
\pgftransformreset% <- I think is possibly vital.
%
% Get bounding box of decorated path as a node.
% Must do it this way using basic layer.
\pgftransformshift{\pgfpointanchor{current path bounding box}{south west}}%
\pgfcoordinate{@1}\pgfpointorigin%
\pgftransformshift{\pgfpointanchor{current path bounding box}{north east}}%
\pgfcoordinate{@2}\pgfpointorigin%
\node [fit=(@1)(@2), inner sep=\railsep+2*\railwidth] (@@) {};
%
% Create a fading for the track.
\pgfinterruptpicture%
\begin{tikzfadingfrompicture}[name=tracks]
\path[draw=transparent!0, line width=\railsep+2*\railwidth,
postaction={draw=transparent!100, line width=\railsep}]
\pgfextra{\pgfsetpath\pgfdecoratedpath};
\useasboundingbox (@@.south west) (@@.north east);
\end{tikzfadingfrompicture}%
\endpgfinterruptpicture
%
% Draw sleepers.
\ifx\sleeperlayer\emptylayer\else\pgfonlayer{\sleeperlayer}\fi%
\draw [draw=\sleepercolor,line width=\sleeperlength, dash pattern=on \sleeperwidth off \sleepersep, every sleeper/.try]
\pgfextra{\pgfsetpath\pgfdecoratedpath};
\ifx\sleeperlayer\emptylayer\else\endpgfonlayer\fi%
%
% Draw the track
\ifx\raillayer\emptylayer\else\pgfonlayer{\raillayer}\fi%
\fill [path fading=tracks, fit fading=false,
fading transform={shift=(@@.center)}, fill=\railcolor]
(@@.south west) rectangle (@@.north east);
\ifx\raillayer\emptylayer\else\endpgfonlayer\fi%
}
}
\def\emptylayer{}
\tikzset{%
track/.style={
decoration=tracks, decorate
},
decorations/.cd,
rail sep/.store in=\railsep,
rail width/.store in=\railwidth,
rail color/.store in=\railcolor,
rail layer/.store in=\raillayer,
sleeper sep/.store in=\sleepersep,
sleeper width/.store in=\sleeperwidth,
sleeper length/.store in=\sleeperlength,
sleeper color/.store in=\sleepercolor,
sleeper layer/.store in=\sleeperlayer,
rail sep=4pt,
rail width=1pt,
rail color=black,
rail layer=rail,
sleeper sep=6pt,
sleeper width=1pt,
sleeper length=10pt,
sleeper color=gray,
sleeper layer=sleeper,
}
\begin{document}
\begin{tikzpicture}
\draw [track] (-2,5) to (0,5) to[out= 0,in=270] (2,8);
\draw [track] (6,5) to (4,5) to[out=180,in=270] (2,8) to (2,10);
\end{tikzpicture}
\end{document}
答案2
我认为这是我最好的建议:
\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.pathmorphing}
\tikzstyle{track}=[
postaction={draw=gray,densely dashed,line width=14pt},
postaction={draw,decorate,decoration={curveto,raise=4pt},line width=2pt},
postaction={draw,decorate,decoration={curveto,raise=-4pt},line width=2pt}]
\begin{document}
\begin{tikzpicture}
\path[track] (-5,27) to[out= 0,in=180] ( 5,33)
to[out= 0,in= 90] ( 7,25)
to[out=270,in= 0] (-5,27);
\path[track] (-4,10) to ( 0,10) to[out= 0,in=270] (4,16);
\path[track] (12,10) to ( 8,10) to[out=180,in=270] (4,16) to (4,20);
\path[track] (-4, 0) to (12, 0);
\path[track] ( 0, 0) to[out= 0,in=270] (3,2)
to[out= 90,in=270] (1,4)
to[out= 90,in=180] (4,6)
to[out= 0,in=120] (8,5)
to[out=300,in= 0] (4,0);
\end{tikzpicture}
\end{document}
如您所见,这在终点处产生了问题,即轨道在中心线相交的地方。通过定义自定义装饰(有点超出我的经验),您可能还可以使线条curveto
从凸起点开始。如果不行,一个粗略的解决方法是通过在终点上方绘制一个白色矩形来擦除终点。
答案3
这是另一种使用的方法blend group
。
\documentclass[border=9,tikz]{standalone}
\begin{document}
\tikz{
\path[save path=\cloud](0,0)..controls(5,5)..(10,0);
\path[save path=\hill](0,5)..controls(0,0)and(10,0)..(10,5);
\begin{scope}[blend group=darken]
\begin{scope}[dash pattern=on2off4,line width=12,darkgray]
\draw[use path=\cloud];
\draw[use path=\hill];
\end{scope}
\begin{scope}[blend group=normal]
\draw[use path=\cloud][line width=8];
\draw[use path=\cloud][line width=6,white];
\end{scope}
\begin{scope}[blend group=normal]
\draw[use path=\hill][line width=8];
\draw[use path=\hill][line width=6,white];
\end{scope}
\end{scope}
}
\end{document}