TikZ:访问装饰路径的坐标?

TikZ:访问装饰路径的坐标?

是否可以访问某些路径的装饰坐标?我画了两条路径,例如:

   \begin{figure}[t]
     \begin{tikzpicture}
       \draw [red,decorate,decoration={crosses, segment length=1.05cm,
           transform={shift only}}]
             {(4, 4) .. controls(6, 3.5) .. (8, 4)};
       \draw [red,decorate,decoration={crosses, segment length=1.05cm,
           transform={shift only}}]
             {(4, 4) .. controls(6, 4.5) .. (8, 4)};
    \end{tikzpicture}
   \end{figure}

每条路径都包含一些十字,我想用箭头将它们连接起来。如果我可以访问十字的坐标,这将非常简单。这可能吗?

答案1

这里有一种方法可以做到这一点,即修改crosses装饰以向每个十字添加标签。您可以选择指定标签的前缀,并且数字索引会随着装饰放置十字而递增。

\documentclass{article}
%\url{http://tex.stackexchange.com/q/65557/86}

\usepackage{tikz}
\usetikzlibrary{decorations.shapes}

\makeatletter
% crosses decoration

\pgfkeys{/pgf/decoration/label cross name/.initial=cross}
\newcount\pgf@lblcross@index

\pgfdeclaredecoration{labelled crosses}{init}
{
  \state{init}[width=+0pt,next state=crosses,persistent
precomputation={
  \pgfkeysgetvalue{/pgf/decoration/label cross name}{\pgf@lblcross}
  \global\pgf@lblcross@index=0\relax
    \pgfmathparse{\pgfkeysvalueof{/pgf/decoration/shape start
width}/2}
    \edef\pgf@lib@dec@ssw{\pgfmathresult pt}
    \pgfmathparse{\pgfkeysvalueof{/pgf/decoration/shape start
height}/2}
    \edef\pgf@lib@dec@ssh{\pgfmathresult pt}
  }]{}  
  \state{crosses}[switch if less than=+\pgfdecorationsegmentlength to
last,
                  width=+\pgfdecorationsegmentlength]
  {
    \pgfcoordinate{\pgf@lblcross-\the\pgf@lblcross@index}{\pgfpointorigin}
    \global\advance\pgf@lblcross@index by 1\relax
    \pgfpathmoveto{\pgfqpoint{-\pgf@lib@dec@ssw}{\pgf@lib@dec@ssh}}
    \pgfpathlineto{\pgfqpoint{\pgf@lib@dec@ssw}{-\pgf@lib@dec@ssh}}
    \pgfpathmoveto{\pgfqpoint{-\pgf@lib@dec@ssw}{-\pgf@lib@dec@ssh}}
    \pgfpathlineto{\pgfqpoint{\pgf@lib@dec@ssw}{\pgf@lib@dec@ssh}}
  }
  \state{last}[width=+\pgfdecoratedremainingdistance]
  {
    \pgfcoordinate{\pgf@lblcross-\the\pgf@lblcross@index}{\pgfpointorigin}
    \global\advance\pgf@lblcross@index by 1\relax
    \pgfpathmoveto{\pgfqpoint{-\pgf@lib@dec@ssw}{\pgf@lib@dec@ssh}}
    \pgfpathlineto{\pgfqpoint{\pgf@lib@dec@ssw}{-\pgf@lib@dec@ssh}}
    \pgfpathmoveto{\pgfqpoint{-\pgf@lib@dec@ssw}{-\pgf@lib@dec@ssh}}
    \pgfpathlineto{\pgfqpoint{\pgf@lib@dec@ssw}{\pgf@lib@dec@ssh}}
  }
  \state{final}{
    \pgfpathmoveto{\pgfpointdecoratedpathlast}
  }
}

\makeatother

\begin{document}
\begin{tikzpicture}
       \draw [postaction={draw=red,decorate},decoration={labelled
crosses, label cross name=A, segment length=1.05cm,
           transform={shift only}}]
             {(4, 4) .. controls(6, 3.5) .. (8, 4)};
       \draw [postaction={draw=red,decorate},decoration={labelled
crosses, label cross name=B, segment length=1.05cm,
           transform={shift only}}]
             {(4, 4) .. controls(6, 4.5) .. (8, 4)};
             \draw[->,green,dashed] (A-1) to[bend right] (B-2);
             \draw[->,green,dashed] (A-3) to[bend left] (B-3);
    \end{tikzpicture}
\end{document}

得出的结果为:

路径上有标记的十字

(实际上,事实并非如此,但只是因为在生成真实图片后,我认为它太小而看不清发生了什么,所以我把它放大了,但我懒得更改上面的代码。)

相关内容