为装饰添加垂直移位(跟进 Q18617)

为装饰添加垂直移位(跟进 Q18617)

我正在研究@Caramdir 的回答TikZ:仅绘制给定路径的某个中心长度

我希望该段相对于路径正交移动。我试图raise在某处插入

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations}

% A simple empty decoration, that is used to ignore the last bit of the path                                                                  
\pgfdeclaredecoration{ignore}{final}
                     {
                       \state{final}{}
                     }

                     % Declare the actual decoration.                                                                                         
                     \pgfdeclaremetadecoration{middle}{initial}{
               \state{initial}[
                         width={(\pgfmetadecoratedpathlength - \the\pgfdecorationsegmentlength)/2},
                         next state=middle
                       ]
                             {\decoration{moveto}}

                     \state{middle}[
                               width={\the\pgfdecorationsegmentlength},
                               next state=final
                             ]
                                   {\decoration{curveto}}

                                   \state{final}
                                         {\decoration{ignore}}
                     }

% Create a key for easy access to the decoration (as suggested by Jake).                                                 
\tikzset{middle segment/.style={decoration={middle},decorate, segment length=#1}}
\begin{document}
\begin{tikzpicture}[radius=2pt]
     \fill (0,0) circle;
     \fill (4,2) circle;
     \draw[middle segment=2cm, double, red, ->] (0,0) to[bend right =20] (4,1);
     \end{tikzpicture}
\end{document}

答案1

您可以简单地使用 calc 库来移动路径。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations,calc}

% A simple empty decoration, that is used to ignore the last bit of the path                                                                  
\pgfdeclaredecoration{ignore}{final}
                     {
                       \state{final}{}
                     }

                     % Declare the actual decoration.                                                                                         
                     \pgfdeclaremetadecoration{middle}{initial}{
               \state{initial}[
                         width={(\pgfmetadecoratedpathlength - \the\pgfdecorationsegmentlength)/2},
                         next state=middle
                       ]
                             {\decoration{moveto}}

                     \state{middle}[
                               width={\the\pgfdecorationsegmentlength},
                               next state=final
                             ]
                                   {\decoration{curveto}}

                                   \state{final}
                                         {\decoration{ignore}}
                     }

% Create a key for easy access to the decoration (as suggested by Jake).                                                 
\tikzset{middle segment/.style={decoration={middle},decorate, segment length=#1}}
\begin{document}
\begin{tikzpicture}[radius=2pt]
     \fill (0,0) circle;
     \fill (4,2) circle;
     \draw[middle segment=2cm, red, ->] (0,0) to[bend right =20] (4,1);
     \draw[middle segment=2cm, red, ->] ($(0,0)!1cm!90:(4,1)$) to[bend right =20] 
     ($(4,1)!1cm!-90:(0,0)$);
     \end{tikzpicture}
\end{document}

相关内容