阅读这个问题:模拟手绘线条按照 Percusse 的想法,我尝试了这个:
但是,是否可以在样式命令中“隐藏”多行代码并通过传递参数keys
。顺便说一句,对于step
0.01 的 a,有 101 个标记,但对于step
0.2 的 a,只有 50 个标记,并且该行不会到达末尾?
\documentclass[tikz]{standalone}
\usepackage{}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}[decoration={markings,
mark=between positions 0 and 1 step .01 with {
\node at (0,.3pt*rand) [coordinate,
name=mark-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}]
{};}}]
\draw [help lines] grid (3,2);
\draw [postaction={decorate},help lines] (0,0) -- (3,1) arc (0:180:1.5 and 1);
\draw[smooth] [red] (mark-1)
\foreach \i in {2,...,101} {-- (mark-\i) } ;
\end{tikzpicture}
\begin{tikzpicture}[decoration={markings,
mark=between positions 0 and 1 step .01 with {
\node at (0,.4pt*rand) [coordinate,
name=mark-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}]
{};}}]
\draw [help lines] grid (3,2);
\path [postaction={decorate}] (0,0) -- (3,1) arc (0:180:1.5 and 1) -- cycle ;
\draw[smooth,red,fill=red!15] (mark-1)
\foreach \i in {2,...,101} {-- (mark-\i) } ;
\end{tikzpicture}
\end{document}
答案1
这是满足您要求的一种方法。可能不是最好的。
\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.markings}
\tikzset{%
hand drawn/random interval/.initial = 1pt,
hand drawn/step/.initial = 0.05,
hand drawn with markings/.style = {%
decoration={%
markings,
mark = between positions 0 and 1 step \pgfkeysvalueof{/tikz/hand drawn/step} with {%
\edef\SequenceNumber{%
\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}}%
\coordinate (mark-\SequenceNumber) at
(0,\pgfkeysvalueof{/tikz/hand drawn/random interval}*rand);
\ifnum\SequenceNumber>1\relax
\draw[#1] (mark-\number\numexpr\SequenceNumber-1\relax)
--
(mark-\SequenceNumber);
\fi},
},
decorate},
}
\begin{document}
\begin{tikzpicture}
\draw [help lines] grid (3,2);
\draw [hand drawn with markings = {red}] (0,0) -- (3,1) arc (0:180:1.5 and 1);
\end{tikzpicture}
\end{document}