使用 TikZ 制作高速公路

使用 TikZ 制作高速公路

考虑以下 MWE:

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{calc}
\tikzset{
    car/.style={
        scale=.5,minimum width=.1cm,minimum height=.5cm,fill=#1
    }
}
\begin{document}
\foreach \n in {0,10,...,360,350,340,...,0}
{
    \begin{tikzpicture}
        \fill[green!60!black] ([xshift=1mm,yshift=-1mm]1,0) rectangle ([xshift=-1mm,yshift=1mm]4,-3);
            \path ([xshift=1mm,yshift=-1mm]1,0) -- ([xshift=-1mm,yshift=1mm]4,-3) node[midway,fill=green!40!black,circle,inner sep=.3cm] (a) {};
        \node[fill=green!40!black,circle,inner sep=.1cm,above left=.5cm] at (a) {};
        \node[fill=green!40!black,circle,inner sep=.1cm,above right=.5cm] at (a) {};
        \node[fill=green!40!black,circle,inner sep=.1cm,below left=.5cm] at (a) {};
        \node[fill=green!40!black,circle,inner sep=.1cm,below right=.5cm] at (a) {};
            \draw[double distance=2mm,double=gray] (5,0) -- (0,0) arc(-90:-360:1) --+ (0,-5) arc(0:-270:1) --+ (5,0) arc(90:-180:1) --+ (0,5) arc(180:-90:1);
            \draw[white,densely dashed] (5,0) -- (0,0) arc(-90:-360:1) --+ (0,-5) arc(0:-270:1) --+ (5,0) arc(90:-180:1) --+ (0,5) arc(180:-90:1);
                \node[shift={(0,1)},car=red,rotate=\n] at (\n:1) {};
                \node[shift={(5,1)},car=blue,rotate=-\n] at (-\n:1) {};
                \node[shift={(0,-4)},car=green,rotate=-\n] at (-\n:1) {};
                \node[shift={(5,-4)},car=yellow,rotate=\n] at (\n:1) {};
        \pgfmathsetmacro\factor{\n/360}
            \node[rotate=-90,car=purple] at ($(0,0)!\factor!(5,0)$) {};
            \node[rotate=-90,car=violet] at ($(5,-3)!\factor!(0,-3)$) {};
                \node[car=brown] at ($(4,1)!\factor!(4,-4)$) {};
                \node[car=orange] at ($(1,-3)!\factor!(1,0)$) {};
    \end{tikzpicture}
}
\end{document}

输出如下:

截屏

我的问题是:我怎样才能让车子沿着轨道的“弧线部分”移动?我本来可以用\ifnum,但我觉得太费力了……

答案1

只是为了好玩:一种将汽车置于道路任意位置的简单方法。为了让@DavidCarlisle高兴,汽车在道路的错误一侧行驶。

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{calc,decorations.markings}
\tikzset{
    car/.style={
        scale=.5,minimum width=.1cm,minimum height=.5cm,fill=#1
    },
    car on road/.style args={at pos #1 with dist #2 and color #3}{decorate,decoration={markings,
    mark=at position #1 with {\path (-2pt,#2) coordinate (aux1) 
    (2pt,#2) coordinate (aux2);
    \pgftransformreset
    \path let \p1=($(aux2)-(aux1)$),\n1={90+atan2(\y1,\x1)} in
    ($(aux2)!0.5!(aux1)$) node[car=#3,rotate=\n1]{};}}}
}
\begin{document}
\def\roadpath{(5,0) -- (0,0) arc(-90:-360:1) --+ (0,-5) arc(0:-270:1) --+ (5,0) arc(90:-180:1) --+ (0,5) arc(180:-90:1)}
\foreach \n in {0,5,...,355}
{
    \begin{tikzpicture}
        \fill[green!60!black] ([xshift=1mm,yshift=-1mm]1,0) rectangle ([xshift=-1mm,yshift=1mm]4,-3);
            \path ([xshift=1mm,yshift=-1mm]1,0) -- ([xshift=-1mm,yshift=1mm]4,-3) node[midway,fill=green!40!black,circle,inner sep=.3cm] (a) {};
        \node[fill=green!40!black,circle,inner sep=.1cm,above left=.5cm] at (a) {};
        \node[fill=green!40!black,circle,inner sep=.1cm,above right=.5cm] at (a) {};
        \node[fill=green!40!black,circle,inner sep=.1cm,below left=.5cm] at (a) {};
        \node[fill=green!40!black,circle,inner sep=.1cm,below right=.5cm] at (a) {};
            \draw[double distance=3mm,double=gray] \roadpath;
            \draw[white,densely dashed] \roadpath;
        \pgfmathsetmacro\factor{\n/360}
        \path[car on road=at pos {\factor} with dist 1mm and color red] \roadpath;
        \path[car on road=at pos {1-\factor} with dist -1mm and color blue] \roadpath;
        \pgfmathsetmacro\factor{mod(\n/360+0.5,1)}
        \path[car on road=at pos {\factor} with dist 1mm and color cyan] \roadpath;
        \path[car on road=at pos {1-\factor} with dist -1mm and color orange] \roadpath;
    \end{tikzpicture}
}
\end{document}

在此处输入图片描述

相关内容