沿“多节点路径”移动点

沿“多节点路径”移动点

我想创建一个 gif (正如这个答案所解释的那样) 沿弯曲的“多节点路径”移动的点。我希望此路径指定如下:由边连接的节点序列。在迭代中,我希望沿此“多节点路径”指定节点,如下所示node[pos=\n, above] {x}

我有一个解决方案,但它不易于扩展:如果我想在路径上添加更多节点,我必须手动更改它。此外,当点沿路径移动时,窗口会上下移动,这也是一个问题。

在此处输入图片描述

我可以通过拥有与边数一样多的 for 循环并node[pos=\n, above] {x}每次将其放在相关边上来做到这一点,但我想要一个更通用的解决方案,这样我就不必提前知道“多节点路径”会有多长,如下所示(不起作用):

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}

\foreach \n in {0,0.05,...,1} {
\begin{tikzpicture}
\draw[name path=mypath]
    (0,0)   -- ++ (1,2)
            -- ++ (1,.5)
            -- ++ (1,-1)
            -- ++ (1,0)
            -- ++ (1,-4);
\draw (mypath) node [draw=black, fill=red, circle] {}
               node [pos=\n, above] {x};
\end{tikzpicture}
}

\end{document}

我当前的解决方案很糟糕:(:

\documentclass[tikz, border=.5cm]{standalone}
\usepackage{tikz}
\begin{document}

\foreach \n in {0,0.1,...,.9} {
\begin{tikzpicture}
    \draw
        (0,0)   -- node [circle, fill=red, draw=black, pos=\n, inner sep = 0, minimum size = 4pt] {} node [pos=\n, above] {x} ++ (1,2)
                -- ++ (1,.5)
                -- ++ (1,-1)
                -- ++ (1,0)
                -- ++ (1,-4);
\end{tikzpicture}
}

\foreach \n in {0,0.1,...,.9} {
\begin{tikzpicture}
    \draw
        (0,0)   -- ++ (1,2)
                -- node [circle, fill=red, draw=black, pos=\n, inner sep = 0, minimum size = 4pt] {} node [pos=\n, above] {x} ++ (1,.5)
                -- ++ (1,-1)
                -- ++ (1,0)
                -- ++ (1,-4);
\end{tikzpicture}
}

\foreach \n in {0,0.1,...,.9} {
\begin{tikzpicture}
    \draw
        (0,0)   -- ++ (1,2)
                -- ++ (1,.5)
                -- node [circle, fill=red, draw=black, pos=\n, inner sep = 0, minimum size = 4pt] {} node [pos=\n, above] {x} ++ (1,-1)
                -- ++ (1,0)
                -- ++ (1,-4);
\end{tikzpicture}
}

\foreach \n in {0,0.1,...,.9} {
\begin{tikzpicture}
    \draw
        (0,0)   -- ++ (1,2)
                -- ++ (1,.5)
                -- ++ (1,-1)
                -- node [circle, fill=red, draw=black, pos=\n, inner sep = 0, minimum size = 4pt] {} node [pos=\n, above] {x} ++ (1,0)
                -- ++ (1,-4);
\end{tikzpicture}
}

\foreach \n in {0,0.1,...,.9} {
\begin{tikzpicture}
    \draw
        (0,0)   -- ++ (1,2)
                -- ++ (1,.5)
                -- ++ (1,-1)
                -- ++ (1,0)
                -- node [circle, fill=red, draw=black, pos=\n, inner sep = 0, minimum size = 4pt] {} node [pos=\n, above] {x} ++ (1,-4);
\end{tikzpicture}
}

\begin{tikzpicture}
    \draw
        (0,0)   -- ++ (1,2)
                -- ++ (1,.5)
                -- ++ (1,-1)
                -- ++ (1,0)
                -- node [circle, fill=red, draw=black, pos=1, inner sep = 0, minimum size = 4pt] {} node [pos=1, above] {x} ++ (1,-4);
\end{tikzpicture}

\end{document}

答案1

使用decorations.markingsoverlay选项。该overlay选项假定对象的大小为零,因此不会添加到边界框。由于节点现在超出了路径的边界框,因此您必须在绘图周围添加边框,以免它从页面上掉下来。

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}

\foreach \n in {0,0.05,...,1} {
    \begin{tikzpicture}
    \draw
        [postaction=decorate,
         decoration={
            markings,
            mark=at position \n with {
                \draw [overlay, draw=black, fill=red] circle (2pt);
                \node [overlay, above] {x};
            }
         }]
        (0,0)   -- ++ (1,2)
                -- ++ (1,.5)
                -- ++ (1,-1)
                -- ++ (1,0)
                -- ++ (1,-4);
    \end{tikzpicture}
}

\end{document}

在此处输入图片描述

答案2

非常类似于Henri Menke 的精彩回答倾斜度很小:记录边界框,并应用所有迭代中最大的边界框。需要编译两次才能正常工作。代码更复杂,但边界框只会在需要的方向上增加。此代码有点类似于这个很好的答案它解决了类似的问题。

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.markings,calc}
\begin{document}
\pgfmathsetmacro{\xmin}{0}
\pgfmathsetmacro{\xmax}{0}
\pgfmathsetmacro{\ymin}{0}
\pgfmathsetmacro{\ymax}{0}
\foreach \X in {0,0.05,...,1} {
    \begin{tikzpicture}
    \ifdefined\figbb\relax
    \path \figbb;
    \fi
    \draw
        [postaction=decorate,
         decoration={
            markings,
            mark=at position \X with {
                \node [circle,inner sep=2pt,draw,fill=red,label=above:$x$]{};
            }
         }]
        (0,0)   -- ++ (1,2)
                -- ++ (1,.5)
                -- ++ (1,-1)
                -- ++ (1,0)
                -- ++ (1,-4);
    \path let \p1=(current bounding box.south west),
    \p2=(current bounding box.north east)
    in \pgfextra{%
    \pgfmathsetmacro{\xmin}{min(\x1,\xmin)}
    \pgfmathsetmacro{\xmax}{max(\x2,\xmax)}
    \pgfmathsetmacro{\ymin}{min(\y1,\ymin)}
    \pgfmathsetmacro{\ymax}{max(\y2,\ymax)}
    \xdef\xmin{\xmin pt}
    \xdef\xmax{\xmax pt}    
    \xdef\ymin{\ymin pt}
    \xdef\ymax{\ymax pt}    
    };
    \end{tikzpicture}
}
\makeatletter               
\edef\figbb{(\xmin,\ymin) rectangle (\xmax,\ymax)}
\immediate\write\@mainaux{\xdef\string\figbb{\figbb}\relax}
\makeatother
\end{document}

在此处输入图片描述

相关内容