26

I have drawn a simple car with this code:

\begin{tikzpicture}
    \shade[top color=red, bottom color=white, shading angle={135}]
        [draw=black,fill=red!20,rounded corners=1.2ex,very thick] (1.5,.5) rectangle (6.5,2.5);
    \draw[draw=black,fill=gray!50,thick] (3,.5) circle (.5);
    \draw[draw=black,fill=gray!50,thick] (5,.5) circle (.5);    
    \draw[draw=black,fill=gray!80,semithick] (3,.5) circle (.4);
    \draw[draw=black,fill=gray!80,semithick] (5,.5) circle (.4);

    \draw[->,semithick] (0,-.5) -- (0,3);
    \draw (0,3.5) node {$r[x(t)]$};
    \draw[->,semithick] (-.5,0) -- (8,0);
    \draw (8.5,0) node {$x(t)$};
\end{tikzpicture}

And this is the result:

My simple car

There is a simple way to define the outline more similar to a car?

Thanks to all.

3
  • 1
    I would probably load an SVG picture in InkScape (probably obtained from this server: openclipart.org/search/?query=car) and convert it to TikZ (github.com/kjellmf/svg2tikz) after I am done with modifying it.
    – Malipivo
    Commented Mar 28, 2015 at 10:18
  • OK. I try and... if it works I post the result. Commented Mar 28, 2015 at 10:20
  • 1
    It looks pretty nice actually. A quick try for another path is (0.5,.5) |- ++(1,1) --++(30:1.5) to[bend left,looseness=0.3]++(2,0)--++(-25:2) --++(2,-0.3)--++(-90:0.8)--cycle. You move the wheels yourself :)
    – percusse
    Commented Mar 28, 2015 at 10:35

2 Answers 2

28

A vintage car:

\documentclass{article}
\usepackage{tikz}

\begin{document}
  \begin{tikzpicture}
    \shade[top color=red, bottom color=white, shading angle={135}]
        [draw=black,fill=red!20,rounded corners=1.2ex,very thick] (1.5,.5) -- ++(0,1) -- ++(1,0.3) --  ++(3,0) -- ++(1,0) -- ++(0,-1.3) -- (1.5,.5) -- cycle;
    \draw[very thick, rounded corners=0.5ex,fill=black!20!blue!20!white,thick]  (2.5,1.8) -- ++(1,0.7) -- ++(1.6,0) -- ++(0.6,-0.7) -- (2.5,1.8);
    \draw[thick]  (4.2,1.8) -- (4.2,2.5);
    \draw[draw=black,fill=gray!50,thick] (2.75,.5) circle (.5);
    \draw[draw=black,fill=gray!50,thick] (5.5,.5) circle (.5);
    \draw[draw=black,fill=gray!80,semithick] (2.75,.5) circle (.4);
    \draw[draw=black,fill=gray!80,semithick] (5.5,.5) circle (.4);

    \draw[->,semithick] (0,-.5) -- (0,3);
    \draw (0,3.5) node {$r[x(t)]$};
    \draw[->,semithick] (-.5,0) -- (8,0);
    \draw (8.5,0) node {$x(t)$};
\end{tikzpicture}
\end{document}

enter image description here

6
  • Thanks a lot. I have used this car (openclipart.org/detail/4454/beetle-car) and generate the PDF via Inkskape. But your is a car, mine is... a bus? :-) Commented Mar 28, 2015 at 11:36
  • 6
    To paraphrase Henry Ford, "you can have a car in any color you want, as long as its red." Commented Mar 28, 2015 at 11:52
  • Infact, I have changed the color of the car from green to red. :-) Commented Mar 28, 2015 at 13:26
  • the problem is when you want the car facing other directions, you have to set again all nodes
    – L F
    Commented Feb 3, 2016 at 1:44
  • @LuisFelipe: This is just a prototype. For general usage, I would use a pic and that simplifies it all. :)
    – user11232
    Commented Mar 27, 2016 at 3:27
19

Here's a trolley bus / tram in case you are open to public transportation:

trolley bus tram

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{positioning,backgrounds}

\begin{document}
  \begin{tikzpicture}
    [
      olwyn/.style={circle, draw, thick, minimum width=5pt, inner sep=0pt, fill=gray!75},
      ]
    \path [draw, thick]
      coordinate (trolley) ++(-.2,2.75pt) node (w1) [olwyn] {}
        ++(.425,0) node (w2) [olwyn] {}
        ++(.425,0pt) node (w3) [olwyn] {};
    \path [draw, fill]
      (w1) circle (1pt)
      (w2) circle (1pt)
      (w3) circle (1pt);
    \path [draw, left color=red!75!black, right color=red!75!black!20]
      (trolley) ++(-.4,1.5pt) coordinate (t1) |- ++(1.25,.35) coordinate (t2) |- (w3.south east -| w3.east) |- (w3.west) |- (w2.south east -| w2.east) |- (w2.west) |- (w1.south east -| w1.east) |- (w1.west) -- (w1.south west -| w1.west) -| cycle
      (t1 -| t2) ++(-.05pt,4pt) coordinate (t3) |- ++(-.175,.175) coordinate (t4) |- cycle
      (t3 -| t4) ++(-.5pt,0) |- ++(-.25,.175) coordinate (t5) |- cycle
      (t3 -| t5) ++(-.5pt,0) |- ++(-.25,.175) coordinate (t6) |- cycle
      (t3 -| t6) ++(-.5pt,0) |- ++(-.25,.175) coordinate (t7) |- cycle
      (t3 -| t7) ++(-.5pt,0) |- ++(-.25,.175) coordinate (t8) |- cycle;
    \scoped [on background layer] \fill [left color=gray!75!blue!50, right color=gray!75!blue!10] (t3) rectangle (t8);
  \end{tikzpicture}
\end{document}

EDIT

I just turned this into a TikZ library for my own use. As libraries go, it is extremely simplistic. It does just one thing: offers a pic called tram. But I thought I might as well post it:

\ProvidesFile{tikzlibrarytrambws.code.tex}
\usetikzlibrary{backgrounds}
\colorlet{lliwytram}{red!75!black}
\colorlet{lliwffenestri}{gray!75!blue}
\colorlet{lliwolwynion}{gray!75}
\tikzset{
  lliw y tram/.code={
        \colorlet{lliwytram}{#1}
  },
  lliw ffenestri/.code={
        \colorlet{lliwffenestri}{#1}
  },
  lliw olwynion/.code={
        \colorlet{lliwolwynion}{#1}
  },
  olwyn/.style={circle, draw, line width=#1*.8pt, minimum width=#1*5pt, inner sep=0pt, fill=lliwolwynion},
  pics/tram/.style={
        code={
          \path [draw, line width=#1*.8pt]
          coordinate (tram) ++(-.2*#1,#1*2.75pt) node (w1) [olwyn=#1] {}
                  ++(#1*.425,0) node (w2) [olwyn=#1] {}
                  ++(#1*.425,0) node (w3) [olwyn=#1] {};
          \path [draw, fill, line width=#1*.4pt]
                (w1) circle (#1*1pt)
                (w2) circle (#1*1pt)
                (w3) circle (#1*1pt);
          \path [draw, left color=lliwytram, right color=lliwytram!20, line width=#1*.4pt]
                (tram) ++(-.4*#1,#1*1.5pt) coordinate (t1) |- ++(#1*1.25,#1*.35) coordinate (t2) |- (w3.south east -| w3.east) |- (w3.west) |- (w2.south east -| w2.east) |- (w2.west) |- (w1.south east -| w1.east) |- (w1.west) -- (w1.south west -| w1.west) -| cycle
                (t1 -| t2) ++(-.05*#1pt,#1*4pt) coordinate (t3) |- ++(-.175*#1,#1*.175) coordinate (t4) |- cycle
                (t3 -| t4) ++(-.5*#1pt,0) |- ++(-.25*#1,.175*#1) coordinate (t5) |- cycle
                (t3 -| t5) ++(-.5*#1pt,0) |- ++(-.25*#1,.175*#1) coordinate (t6) |- cycle
                (t3 -| t6) ++(-.5*#1pt,0) |- ++(-.25*#1,.175*#1) coordinate (t7) |- cycle
                (t3 -| t7) ++(-.5*#1pt,0) |- ++(-.25*#1,.175*#1) coordinate (t8) |- cycle;
          \scoped [on background layer] \fill [left color=lliwffenestri!50, right color=lliwffenestri!10] (t3) rectangle (t8);
        }
  }
}
\endinput

To draw a tram, you say

\pic {tram=1};

where 1 is a scaling factor. So 2 will draw a tram twice as large and .5 will draw a tram twice as small.

If you want to change the colouring, you can use

lliw y tram=<colour of tram>
lliw olwynion=<colour of wheels>
lliw ffenestri=<colour of windows>

The defaults are red!75!black, gray!75 and gray!75!blue respectively.

For example:

\documentclass[tikz,border=5pt,11pt]{standalone}
\usetikzlibrary{trambws}

\begin{document}
  \begin{tikzpicture}
    \pic  at (0,.5) {tram=.5};
    \pic [lliw y tram=blue] {tram={1}};
    \pic [lliw olwynion=green!50!gray, lliw ffenestri=green!25!white!75!gray, lliw y tram=green!75!black] at (0,-1) {tram=2};
  \end{tikzpicture}
\end{document}

various trams

6
  • Have you read my paper? If no, my experiments was start with two public buses. :-) Commented Mar 28, 2015 at 16:28
  • 2
    @GiacomoAlessandroni No. I'm afraid not. This is adapted from diagrams I use when teaching 'trolleyology'! Do you have a link for your paper?
    – cfr
    Commented Mar 28, 2015 at 16:30
  • Being red, where's the double decker? It is in London, right? Commented Mar 28, 2015 at 18:03
  • 1
    San Fran. Ok, I can get on board with that. ;^) Commented Mar 28, 2015 at 18:38
  • 1
    @cfr You can freely download the paper SmartRoadSense: Collaborative Road Surface Condition Monitoring. In the team I am the author of mathematical model. And thanks for your interest. Commented Mar 29, 2015 at 15:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .