我用这个代码画了一辆简单的汽车:
\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}
结果如下:
有没有简单的方法可以定义更类似于汽车的轮廓?
谢谢大家。
答案1
一辆老式汽车:
\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}
答案2
如果您愿意乘坐公共交通,这里有一辆无轨电车/有轨电车:
\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}
编辑
我刚刚把它变成了一个 TikZ 库供自己使用。作为库,它非常简单。它只做一件事:提供一个pic
调用tram
。但我想我也可以发布它:
\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
要画一辆有轨电车,你可以说
\pic {tram=1};
其中1
是缩放因子。因此,2
将绘制一辆大两倍的电车,.5
并将绘制一辆小两倍的电车。
如果你想改变颜色,你可以使用
lliw y tram=<colour of tram>
lliw olwynion=<colour of wheels>
lliw ffenestri=<colour of windows>
默认值分别为red!75!black
、gray!75
和gray!75!blue
。
例如:
\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}