21

Consider the following 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}

Here is the output:

Screenshot

My questions is: How can I make the cars to move along those “arc-parts“ of the track? I would have work with \ifnum, but I think it is too laborius …

1
  • 1
    Can't you just define the full thing to be one path and then place the cars via decorations.matkings?
    – user121799
    Nov 4, 2018 at 18:30

1 Answer 1

20

Just for fun: a simple way to put the car at an arbitrary position of the road path. To make @DavidCarlisle happy, cars drive on the wrong side of the road.

\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}

enter image description here

4
  • 8
    +1 for the wrong side of the road :-D
    – egreg
    Nov 4, 2018 at 22:07
  • 4
    @egreg Of course, in Italy they drive on the right side. And since in the UK they drive on the opposite side, and since the opposite of right is wrong, there is only one possible conclusion ;-)
    – user121799
    Nov 4, 2018 at 22:09
  • 1
    @marmot Very nice comments. +1.
    – Sebastiano
    Nov 5, 2018 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.