# Draw a car profile with TikZ

4,763

## Solution 1

A vintage car:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
[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}


## Solution 2

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

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


Share:
4,763

Author by

### Giacomo Alessandroni

Updated on August 01, 2022

• Giacomo Alessandroni over 1 year

I have drawn a simple car with this code:

\begin{tikzpicture}
[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:

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

Thanks to all.

• Malipivo over 8 years
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.
• Giacomo Alessandroni over 8 years
OK. I try and... if it works I post the result.
• percusse over 8 years
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 :)
• Giacomo Alessandroni over 8 years
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? :-)
• Steven B. Segletes over 8 years
To paraphrase Henry Ford, "you can have a car in any color you want, as long as its red."
• Giacomo Alessandroni over 8 years
Infact, I have changed the color of the car from green to red. :-)
• Giacomo Alessandroni over 8 years
• cfr over 8 years
@GiacomoAlessandroni No. I'm afraid not. This is adapted from diagrams I use when teaching 'trolleyology'! Do you have a link for your paper?
• Steven B. Segletes over 8 years
Being red, where's the double decker? It is in London, right?
• cfr over 8 years
@StevenB.Segletes Well, it is supposed to be a tram really. So not London. Sheffield (tram) or San Francisco (trolley), maybe?
• Steven B. Segletes over 8 years
San Fran. Ok, I can get on board with that. ;^)
• Giacomo Alessandroni over 8 years
@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.
• L F almost 8 years
the problem is when you want the car facing other directions, you have to set again all nodes
@LuisFelipe: This is just a prototype. For general usage, I would use a pic and that simplifies it all. :)