在哪些情况下我们可以发现 tikz 键“在开始时执行”有用

在哪些情况下我们可以发现 tikz 键“在开始时执行”有用

在 tikz/pgf 手册中,我们可以在“14.13 路径操作”小节中看到有关该密钥的内容,/tikz/execute at begin to但没有任何关于如何具体使用它的示例。因此,我要求提供一些示例,以说明此密钥在实际情况下如何有用。因此,请给出一个例子。

答案1

好的,这是一个相当简单但可能并非完全毫无意义的例子,它绘制一条与您想要绘制的线平行的线。

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\tikzset{
illustrate at begin to/.style=
    {to path={
      (\tikztostart) -- (\tikztotarget) \tikztonodes
    },
    execute at begin to={
    \draw[blue] ($(\tikztostart)!5pt!90:(\tikztotarget)$) --
    ($(\tikztotarget)!5pt!-90:(\tikztostart)$) node[midway,above,sloped]{I'm parallel};}}
}

\begin{document}
\begin{tikzpicture}
\draw[illustrate at begin to] (0,0) to (5,1);
\draw[illustrate at begin to] (0,-2) to (4,-3);
\end{tikzpicture}
\end{document}

在此处输入图片描述

只是为了好玩,另一个应用程序,使用杰克的完整正弦并受到(我的解释)的激励这个问题。(不,如果您感到疑惑的话,这个技巧不适用于曲线路径。;-)

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations,calc}
\pgfdeclaredecoration{complete sines}{initial}
{
    \state{initial}[
        width=+0pt,
        next state=sine,
        persistent precomputation={\pgfmathsetmacro\matchinglength{
            \pgfdecoratedinputsegmentlength / int(\pgfdecoratedinputsegmentlength/\pgfdecorationsegmentlength)}
            \setlength{\pgfdecorationsegmentlength}{\matchinglength pt}
        }] {}
    \state{sine}[width=\pgfdecorationsegmentlength]{
        \pgfpathsine{\pgfpoint{0.25\pgfdecorationsegmentlength}{0.5\pgfdecorationsegmentamplitude}}
        \pgfpathcosine{\pgfpoint{0.25\pgfdecorationsegmentlength}{-0.5\pgfdecorationsegmentamplitude}}
        \pgfpathsine{\pgfpoint{0.25\pgfdecorationsegmentlength}{-0.5\pgfdecorationsegmentamplitude}}
        \pgfpathcosine{\pgfpoint{0.25\pgfdecorationsegmentlength}{0.5\pgfdecorationsegmentamplitude}}
}
    \state{final}{}
}

\begin{document}
\tikzset{fancy sines/.style={#1,decoration={
            complete sines,
            segment length=3mm,
            amplitude=1mm
        },decorate,to path={
      (\tikztostart) -- 
      ($(\tikztotarget)!{-5*\pgflinewidth}!-135:(\tikztostart)$)
      (\tikztotarget) \tikztonodes
    },
    execute at begin to={\foreach \X in {1,...,5}
    {\draw[decorate,#1]
    ($(\tikztostart)!{\X*\pgflinewidth}!45:(\tikztotarget)$) 
    -- ($(\tikztotarget)!{(-5+\X)*\pgflinewidth}!-135:(\tikztostart)$);}}}
}
\begin{tikzpicture}    
\draw[fancy sines=blue] (0,0) to (0,-3);
\draw[fancy sines=red] (0,0) to (3,0);
\draw[fancy sines=green!60!black] (3,0) to (0,-3);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容