我需要制作一系列齿轮系,我已经习惯了\newcommand
参数化齿轮。我确信可以以更有效的方式做到这一点,如果有人能帮我编写更好的代码,那将很有帮助。这比逐行完成要好得多。
\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{patterns,patterns.meta}
% inputs are x, y , diameter of gear
\newcommand{\mygear}[3]{
\draw[very thick] (#1-0,#2+#3/2+0.1) -- (#1-0,#2+0.1);
\draw[very thick] (#1-0.1,#2+#3/2+0.1) -- (#1+0.1,#2+#3/2+0.1);
\draw[very thick] (#1-0.2,#2+0.1) -- (#1+0.2,#2+0.1);
% center of gear
\draw[very thick] (#1-0.2,#2-0.1) -- (#1+0.2,#2-0.1);
\draw[very thick] (#1-0.1,#2-#3/2-0.1) -- (#1+0.1,#2-#3/2-0.1);
\draw[very thick] (#1-0,#2-#3/2-0.1) -- (#1-0,#2-0.1);
}
\begin{document}
\begin{tikzpicture}[blue]
\mygear{1}{5}{2}
\mygear{1}{7.8}{3}
\mygear{2}{5.8}{2}
\mygear{2}{3}{3}
\draw (1,5) -- (1.5,5) |- (2,5.8);
\draw (1,7.8) -- (3,7.8) -- (3,3) -| (2,3);
\end{tikzpicture}
\end{document}
答案1
您的问题无法得到这样的回答,因为使用哪种方法取决于个人喜好。您可以尝试使用图片,这样可以轻松应用样式,并很好地融入 Ti钾我认为 Z 环境:
\documentclass[border=3mm,tikz]{standalone}
\tikzset{
gear upper/.style={
very thick
},
gear lower/.style={
very thick
},
pics/gear/.style={code={
\draw[pic actions, gear upper]
(0,#1/2+0.1) -- (0,0.1)
(-0.1,#1/2+0.1) -- (0.1,#1/2+0.1)
(-0.2,0.1) -- (0.2,0.1);
% center of gear
\draw[pic actions, gear lower]
(-0.2,-0.1) -- (0.2,-0.1)
(-0.1,-#1/2-0.1) -- (0.1,-#1/2-0.1)
(-0,-#1/2-0.1) -- (0,-0.1);
}}
}
\begin{document}
\begin{tikzpicture}[blue]
\pic at (1,5) {gear={2}};
\pic at (1,7.8) {gear={3}};
\pic[red] at (2,5.8) {gear={2}};
\pic[red, gear lower/.append style={densely dotted}] at (2,3) {gear={3}};
\draw (1,5) -- (1.5,5) |- (2,5.8);
\draw (1,7.8) -- (3,7.8) -- (3,3) -| (2,3);
\end{tikzpicture}
\end{document}
使用这种方法,您甚至可以向图片添加一个坐标,以后可以将其用作锚点:
\documentclass[border=3mm,tikz]{standalone}
\tikzset{
gear upper/.style={
very thick
},
gear lower/.style={
very thick
},
pics/gear/.style={code={
\draw[pic actions, gear upper]
(0,#1/2+0.1) -- (0,0.1)
(-0.1,#1/2+0.1) -- (0.1,#1/2+0.1)
(-0.2,0.1) -- (0.2,0.1);
% center of gear
\coordinate (-center) at (0,0);
\draw[pic actions, gear lower]
(-0.2,-0.1) -- (0.2,-0.1)
(-0.1,-#1/2-0.1) -- (0.1,-#1/2-0.1)
(0,-#1/2-0.1) -- (0,-0.1);
}}
}
\begin{document}
\begin{tikzpicture}[blue]
\pic (gear-1) at (1,5) {gear={2}};
\pic (gear-2) at (1,7.8) {gear={3}};
\pic[red] (gear-3) at (2,5.8) {gear={2}};
\pic[red, gear lower/.append style={densely dotted}] (gear-4) at (2,3) {gear={3}};
\draw (gear-1-center) -- ++(0.5,0) |- (gear-3-center);
\draw (gear-2-center) -- ++(2,0) |- (gear-4-center);
\end{tikzpicture}
\end{document}
输出与上面相同。