下图显示一辆汽车正在爬山,但车轮不在正确的位置。我以前有一个矩形,它很容易理解,但我如何移动更复杂的物体?在代码中,你可以看到被注释掉的矩形和新车。我可以为所有 x 坐标添加位移,但我确信有更好的方法。
\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{shapes,arrows, arrows.meta}
\pgfplotsset{compat=newest}
\newcommand{\ang}{30}
\begin{document}
\begin{tikzpicture}
% triangle:
\draw [draw = orange, fill = orange!15] (0,0) coordinate (O) -- (\ang:10)
coordinate [pos=.35] (M) |- coordinate (B) (O);
% angles:
\draw [draw = orange] (O) ++(.8,0) arc (0:\ang:0.8)
node [pos=.4, left] {$\alpha$};
\draw [draw = orange] (B) rectangle ++(-0.3,0.3);
\begin{scope} [-latex,rotate=\ang,every node/.style={rotate=\ang}]
% % Object (rectangle)
% \draw [fill = purple!30,
% draw = purple!50] (M) ++ (0,0.2) rectangle ++ (4.5,1.5);
% Object Car
\draw [fill = purple!30,
draw = purple!50,-] (0,0.2) -- (0,1) -- (1,1) -- (1.5,1.5) -- (2.5,1.5) --(3.2,1)--(4,1)--(4.5,0.7)--(4.5,0.2) --(0,0.2);
% Object (wheel1)
\draw [fill=lightgray] (M) ++ (0.7,0.4) coordinate (w1) circle (.4cm);
% Object (wheel2)
\draw [fill=lightgray] (M) ++ (3.5,0.4) coordinate (w2) circle (.4cm);
% Weight and its projections
\draw [dashed] (M) ++ (2,.9) coordinate (MM) -- ++ (0,-1.29)
node [ right] {$mg\cos{\theta}$};
\draw [dashed] (MM) -- ++ (-0.75,0)
node [midway, above] {$mg\sin{\theta}$};
\draw (MM) -- ++ (-\ang-90:1.5)
node [above left ] {$mg$};
% Normal Force 2
\draw (M) ++ (0.3,-1.1) coordinate (N2) -- ++ (0,1.1)
node [very near start, left] {$N_2$};
% Normal Force 1
\draw (M) ++ (3.5,-1.1) coordinate (N1) -- ++ (0,1.1)
node [very near start, right] {$N_1$};
% Speed
\draw (M) ++ (1.5,2.2) coordinate (w2) -- ++ (0.75,0)
node [above] {$v$};
% Traction force
\draw (M) ++ (-0.8,0) coordinate (TF) -- ++ (1,0)
node [very near start, above] {$F_t$};
\end{scope}
\begin{scope} [rotate=\ang,every node/.style={rotate=\ang}]
\draw [blue] (MM) -- ++ (2.7,0) node [very near end, left] {$ $};
\draw [blue,>=triangle 45, <->] (8,0.9) -- (8,0) node [midway, right] {$h$};;
\draw [blue,>=triangle 45, <->] (3.8,-0.9) -- (5.5,-0.9) node [midway, above] {$b$};;
\draw [blue,>=triangle 45, <->] (MM)++ (0,-1.8) -- (7,-0.9) node [midway, above] {$a$};;
\end{scope}
\end{tikzpicture}
\end{document}
更新通过@blackmild 的输入,我现在已经实现了我想要做的事情。
通过将图片放在标题中:
\tikzset{radiation/.style={{decorate,decoration={expanding waves,angle=90,segment length=4pt}}},
vehicle/.pic={
code={\tikzset{scale=10/10}
\draw [fill = purple!30,
draw = purple!50,-] (-0.2,0.2) -- (-0.2,1) -- (1,1) -- (1.5,1.5) -- (2.5,1.5) --(3.2,1)--(4,1)--(4.5,0.7)--(4.5,0.2) --(-0.2,0.2);
% Object (wheel1)
\draw [fill=lightgray] (0.4,0.4) coordinate (w1) circle (.4cm);
% Object (wheel2)
\draw [fill=lightgray] (3.6,0.4) coordinate (w2) circle (.4cm);
}}
}
并在代码中使用图片。
% Object Car
\path (3.4,0) pic {vehicle};
给出以下结果:
答案1
我使用的另一种方法是使用定义,它允许您创建具有位置、倾斜度、颜色等属性的对象,根据对象的名称放置一些坐标,简而言之,代码中还有其他变体,包括方向的改变,我添加了一个特斯拉模型来测试从 svg 到 tikz 的转换,当然您可以轻松地在 pdf 中导入另一个矢量图,但我决定让代码可移植。
结果:
梅威瑟:
\documentclass[tikz,border=1cm]{standalone}
\usetikzlibrary{shapes,arrows, arrows.meta}
\newcommand{\ang}{30}
\begin{document}
\begin{tikzpicture}[]
%Simplified car [objet like]:\Simplecar(position)[rotation][color]{orientation}{name}
\def\SimpleCar(#1)[#2][#3]#4#5{
\begin{scope}[shift={(#1)},rotate=#2,xscale=#4]
%path(body)
\draw [fill = #3!30,xshift=-10mm] (0,0.2) -- (0,1) -- (1,1) -- (1.5,1.5) -- (2.5,1.5) --(3.2,1)--(4,1)--(4.5,0.7)--(4.5,0.2) --(0,0.2);
% path wheels
\draw [fill=#3!50!gray] (0mm,4mm) circle (4mm);
\draw [fill=#3!50!gray] (25mm,4mm) circle (4mm);
\coordinate (#5-W1) at (0,0);
\coordinate (#5-W2) at (25mm,0);
\coordinate (#5-G) at (12.5mm,10mm);
% Speed
\draw [>=latex,<-](#5-G)++(0.5,0.7) -- ++ (-1,0) node [midway,sloped,anchor=-90] {$v$};
% Traction force
\draw [>=latex,<-](#5-W1) -- ++ (-1,0) node [midway,anchor=90] {$F_t$};
\end{scope}
}
%More detailed car [obtained using inkscape svg to tikz converter]:\MDCar(position)[rotation][color]{name}
\def\MDCar(#1)[#2][#3]#4{
\begin{scope}[shift={(#1)},rotate=#2]
%path(body)
\filldraw[scale=0.5,x=1mm,y=1mm,yscale=-1,xscale=-1,fill=#3!50,xshift=-235,yshift=-90] (59.9429,0.0029) .. controls (58.2798,0.0161) and (56.5224,0.0709) .. (54.6592,0.1699) .. controls (51.8698,0.3182) and (49.2785,0.7036) .. (46.8955,1.2407) .. controls (46.9004,1.2391) and (46.9067,1.2365) .. (46.9116,1.2349) .. controls (35.0588,3.3135) and (25.0020,10.1030) .. (25.0020,10.1030) -- (24.1113,10.1660) .. controls (22.2803,10.1061) and (21.6259,10.2123) .. (17.5122,11.0391) .. controls (15.2265,11.1391) and (13.1653,11.4703) .. (11.3730,11.9180) .. controls (11.2904,11.9383) and (11.2097,11.9609) .. (11.1284,11.9824) .. controls (8.6666,12.6223) and (6.7447,13.4848) .. (5.6074,14.3101) .. controls (2.5699,14.9763) and (0.3984,16.7520) .. (0.3984,16.7520) .. controls (-0.1586,17.2949) and (0.0797,17.2023) .. (0.0044,17.6191) .. controls (-0.0709,18.0360) and (0.7119,21.0322) .. (0.7119,21.0322) .. controls (0.7119,21.0322) and (0.0821,22.9131) .. (0.5215,23.0918) .. controls (0.9609,23.2703) and (1.0903,23.4957) .. (1.4604,24.4233) .. controls (-0.8220,25.6494) and (0.4983,26.3315) .. (1.5059,26.9150) .. controls (2.5136,27.4983) and (5.1650,28.1973) .. (6.5098,27.9229) .. controls (6.4949,27.8726) and (6.4886,27.8209) .. (6.4746,27.7705) -- (8.3862,26.9062) -- (23.4346,26.2646) -- (25.2979,27.3164) .. controls (25.3045,27.3313) and (25.3242,27.3955) .. (25.3242,27.3955) .. controls (25.3242,27.3955) and (25.5918,27.6023) .. (26.2236,27.4849) .. controls (27.8013,27.0856) and (67.5264,26.7188) .. (67.5264,26.7188) .. controls (67.5264,26.7188) and (71.0655,26.7059) .. (72.3955,27.2095) .. controls (72.9263,27.4105) and (73.2239,27.3453) .. (73.4019,27.1245) .. controls (73.7709,27.0085) and (75.1701,26.5817) .. (75.4629,26.5400) .. controls (75.7840,26.4940) and (90.4210,25.8970) .. (90.3750,25.8970) .. controls (90.3293,25.8970) and (92.2559,26.6777) .. (92.2559,26.6777) .. controls (92.2559,26.6777) and (92.3225,26.6082) .. (92.3320,26.5986) .. controls (92.5830,26.6361) and (92.9367,26.6106) .. (93.4336,26.4961) .. controls (95.4068,26.0414) and (96.8291,25.3066) .. (96.8291,25.3066) .. controls (96.8291,25.3066) and (98.1069,23.5919) .. (98.3862,22.9688) .. controls (98.6655,22.3454) and (98.4976,22.1118) .. (98.4976,22.1118) .. controls (98.4976,22.1118) and (98.8375,20.8511) .. (99.2549,19.8252) .. controls (99.6719,18.8000) and (99.6148,18.6385) .. (98.9854,18.0322) .. controls (98.2215,17.0284) and (97.8547,14.8710) .. (98.0010,13.9409) .. controls (98.0616,13.5558) and (98.0431,13.1384) .. (98.0083,12.7661) .. controls (98.0515,11.7298) and (97.7331,10.8516) .. (97.4692,10.3418) .. controls (97.3419,9.9538) and (97.2028,9.5918) .. (97.0620,9.4497) .. controls (96.6727,9.0568) and (97.2353,8.9554) .. (97.7930,8.6543) .. controls (98.3509,8.3530) and (97.8727,8.0535) .. (97.5088,8.0420) .. controls (97.1451,8.0305) and (96.4688,7.9805) .. (96.4688,7.9805) .. controls (95.4388,7.9064) and (92.8843,6.7387) .. (85.3447,4.1309) -- (85.3271,4.1133) .. controls (85.3259,4.1146) and (85.3240,4.1207) .. (85.3228,4.1221) .. controls (85.3044,4.1157) and (85.2943,4.1123) .. (85.2759,4.1060) .. controls (78.6238,1.8073) and (71.5847,-0.0896) .. (59.9429,0.0029) -- cycle;
path(wheel1)
%path wheels
\draw [fill=#3!50!gray] (0,4mm) coordinate (#4.w1) circle (4mm);
\draw [fill=#3!50!gray] (32mm,4mm) coordinate (#4.w2) circle (4mm);
%Special coordinates.
\coordinate (#4-W1) at (0,0);
\coordinate (#4-W2) at (32mm,0);
\coordinate (#4-G) at (16mm,7mm);
% Speed
\draw [>=latex,<-](#4-G)++(0.5,1.2) -- ++ (-1,0) node [midway,anchor=-90] {$v$};
% Traction force
\draw [>=latex,<-](#4-W1) -- ++ (-1,0) node [midway,anchor=90] {$F_t$};
\end{scope}
}
%Another draw code anidation
%Vectors{objet_name}{angle}{direction}
\def\Vectors#1#2#3{
\begin{scope} [>=latex,rotate=\ang,every node/.style={rotate=\ang}]
% Weight and its projections
\draw [dashed,->](#1-G) -- ++ (0,-1.29)node [midway,sloped,anchor=-90,scale=0.9] {$mg\cos{\theta}$};
\draw [dashed,->] (#1-G) -- ++ (-0.75,0) node [midway, sloped,anchor=-90,scale=0.9] {$mg\sin{\theta}$};
\draw [->](#1-G) -- ++ (-\ang-90:1.5) node [midway, sloped,anchor=-90,scale=0.9] {$mg$};
% Normal Force 2
\draw [<-](#1-W1) -- ++ (0,-1.1) node [pos=0.9, anchor=90] {$N_1$};
% Normal Force 1
\draw [<-](#1-W2) -- ++ (0,-1.1) node [pos=0.9, anchor=90] {$N_2$};
\draw [blue] (#1-G) -- ++ (2.7,0) coordinate (temp);
\draw [blue,>=triangle 45, <->] (temp) -- (temp|-#1-W1) node [midway, right] {$h$};
\draw [blue,>=triangle 45, <->] (#1-W2)++(0,-0.9) coordinate (temp) -- (temp-|#1-G) node [midway, above] {$b$};
\draw [blue,>=triangle 45, <->] (#1-W1)++ (0,-0.9) coordinate (temp) -- (temp-|#1-G) node [midway, above] {$a$};;
\end{scope}
}
%Start drawing the thing...
% Hepl lines to work with obsolute coordinates:
\def\wcanvas{15}
\def\hcanvas{12}
\draw[red!5,step=0.25] (-1,-1) grid (\wcanvas,\hcanvas);
\draw[cyan!40,step=1,line width=1] (-1,-1) grid (\wcanvas,\hcanvas);
\foreach \x in {-1, ..., \wcanvas} {%
\node[anchor=90] at (\x,-1) {\tiny\x};
}
\foreach \y in {-1, ..., \hcanvas} {%
\node[anchor=0] at (-1,\y) {\tiny\y};
}
% Terrain
\draw [draw = orange, fill = orange!15,opacity=0.5]
(0mm,0mm) coordinate (O)
-- (\ang:8) coordinate [pos=0.35](M)
-- ++(0:2)
-- ++(\ang:7) coordinate [pos=0.75](N)
|- coordinate (B) (O);
\draw [draw = orange]
(O)
++(.8,0)
arc (0:\ang:0.8) node [pos=.4, left] {$\alpha$};
\draw [draw = orange]
(B)
rectangle ++(-0.3,0.3);
% Starting drawing cars in some absolute and coordinate name positions.
\SimpleCar(0.5,10)[0][purple]{1}{CAR-01}
\MDCar(0.5,7)[0][blue]{CAR-02}
\SimpleCar(N)[\ang][lime]{-1}{CAR-03}
\MDCar(M)[\ang][red]{CAR-04}
\Vectors{CAR-03}{\ang}{->}
\Vectors{CAR-04}{\ang}{->}
\end{tikzpicture}
\end{document}
答案2
这是一条很长的注释。您的代码不够简洁。pgfplots
这里不需要。
对于车轮,您可以weelcenter1
按照从O
到的路径找到第一个车轮的中心M
,然后顺时针旋转 90 度(.4
意味着.4cm
=4mm
车轮的半径)
\path (O)--(M)--([turn]90:.4) coordinate (weelcenter1);
然后你就可以画轮子了
\draw[fill=gray] (weelcenter1) circle(.4);
第二个轮子也类似。
% Object (wheel1)
%\draw [fill=lightgray] (M) ++ (0.7,0.4) coordinate (w1) circle (.4);
% Object (wheel2)
%\draw [fill=lightgray] (M) ++ (3.5,0.4) coordinate (w2) circle (.4);
%\fill[red] (M) circle(.1);
\path
(O)--(M)--([turn]90:.4) coordinate (weelcenter1)
(O)--(M) coordinate[pos=.4] (Mt)
(O)--(Mt)--([turn]90:.4) coordinate (weelcenter2)
;
\draw[fill=gray] (weelcenter1) circle(.4) (weelcenter2) circle(.4);
\fill[red] (weelcenter1) circle(.1) (weelcenter2) circle(.1);