我可以像下面这样绘制单个齿轮,但无法按照以下方式对齐它们。你能帮忙吗?
\documentclass{standalone}
\usepackage{tikz}
\usepackage{amsmath, latexsym, amscd, amsthm}
\newcommand{\gear}[5]{%
\foreach \i in {1,...,#1} {%
[rotate=(\i-1)*360/#1] (0:#2) arc (0:#4:#2) {[rounded corners=1.5pt]
-- (#4+#5:#3) arc (#4+#5:360/#1-#5:#3)} -- (360/#1:#2)
}}
\begin{document}
\begin{tikzpicture}
\draw[ultra thick] \gear{16}{1.6}{1.8}{10}{2};
\end{tikzpicture}
\end{document}
答案1
这应该可以帮助你入门:
代码
\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{arrows}
\newcommand{\gear}[5]{%
\foreach \i in {1,...,#1}
{ [rotate=(\i-1)*360/#1] (0:#2) arc (0:#4:#2) {[rounded corners=0.5pt] -- (#4+#5:#3) arc (#4+#5:360/#1-#5:#3)} -- (360/#1:#2)
}
}
\begin{document}
% given
\pgfmathsetmacro{\rOne}{1.6}% inner radius
\pgfmathsetmacro{\nOne}{16}% num teeth
\pgfmathsetmacro{\nTwo}{24}
\pgfmathsetmacro{\toothHeight}{0.2}
\pgfmathsetmacro{\toothFall}{1}% degrees where radius drops from inner to outer
\pgfmathsetmacro{\aOneTwo}{120}% angle from first to second
% computed
\pgfmathsetmacro{\rTwo}{\rOne*\nTwo/\nOne}% via equating tooth lengths
\pgfmathsetmacro{\ROne}{\rOne+\toothHeight}% outer radius
\pgfmathsetmacro{\RTwo}{\rTwo+\toothHeight}
\pgfmathsetmacro{\dOne}{(360/\nOne-\toothFall)/2}% degrees for a tooth; here inner deg = outer deg
\pgfmathsetmacro{\dTwo}{(360/\nTwo-\toothFall)/2}
\pgfmathsetmacro{\distOneTwo}{\rOne+\rTwo+\toothHeight+0.05}
\begin{tikzpicture}
\draw[thick] \gear{\nOne}{\rOne}{\ROne}{\dOne}{\toothFall};
\draw[thick, shift={(\aOneTwo:\distOneTwo)}, rotate=5.5] \gear{\nTwo}{\rTwo}{\RTwo}{\dTwo}{\toothFall};
\draw[-latex] (\aOneTwo:\rOne/2) -- (\aOneTwo:\rOne);
\draw[-latex, shift={(\aOneTwo:\distOneTwo)}] (\aOneTwo+180:\RTwo/2) -- (\aOneTwo+180:\RTwo);
\end{tikzpicture}
\end{document}
输出
编辑1:如果使用坐标来参考齿轮的中心,您可以很容易地将它们链接在一起。
代码
\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{arrows, calc}
\newcommand{\gear}[5]{%
\foreach \i in {1,...,#1}
{ [rotate=(\i-1)*360/#1] (0:#2) arc (0:#4:#2) {[rounded corners=0.5pt] -- (#4+#5:#3) arc (#4+#5:360/#1-#5:#3)} -- (360/#1:#2)
}
}
\begin{document}
% given
\pgfmathsetmacro{\rOne}{1.6}% inner radius
\pgfmathsetmacro{\nOne}{17}% num teeth
\pgfmathsetmacro{\nTwo}{23}
\pgfmathsetmacro{\nThree}{19}
\pgfmathsetmacro{\toothHeight}{0.2}
\pgfmathsetmacro{\toothFall}{1}% degrees where radius drops from inner to outer
\pgfmathsetmacro{\aOneTwo}{120}% angle from first to second
\pgfmathsetmacro{\aTwoThree}{20}% angle from first to second
% computed
\pgfmathsetmacro{\rTwo}{\rOne*\nTwo/\nOne}% via equating tooth lengths
\pgfmathsetmacro{\rThree}{\rOne*\nThree/\nOne}%
\pgfmathsetmacro{\ROne}{\rOne+\toothHeight}% outer radius
\pgfmathsetmacro{\RTwo}{\rTwo+\toothHeight}
\pgfmathsetmacro{\RThree}{\rThree+\toothHeight}
\pgfmathsetmacro{\dOne}{(360/\nOne-\toothFall)/2}% degrees for a tooth; here inner deg = outer deg
\pgfmathsetmacro{\dTwo}{(360/\nTwo-\toothFall)/2}
\pgfmathsetmacro{\dThree}{(360/\nThree-\toothFall)/2}
\pgfmathsetmacro{\distOneTwo}{\rOne+\rTwo+\toothHeight+0.05}
\pgfmathsetmacro{\distTwoThree}{\rTwo+\rThree+\toothHeight+0.05}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at ($(A)+(\aOneTwo:\distOneTwo)$);
\coordinate (C) at ($(B)+(\aTwoThree:\distTwoThree)$);
\draw[thick, rotate=9] node[align=center] {$\alpha$ \\ \nOne\ Teeth} \gear{\nOne}{\rOne}{\ROne}{\dOne}{\toothFall} ;
\draw[thick, shift={(B)}, rotate=7] node[align=center] {$\beta$ \\ \nTwo\ Teeth} \gear{\nTwo}{\rTwo}{\RTwo}{\dTwo}{\toothFall};
\draw[thick, shift={(C)}, rotate=8] node[align=center] {$\gamma$ \\ \nThree\ Teeth} \gear{\nThree}{\rThree}{\RThree}{\dThree}{\toothFall};
\draw[-latex, very thick] (\aOneTwo:\rOne/2) -- (\aOneTwo:\rOne);
\draw[-latex, very thick, shift={(B)}] (\aOneTwo+180:\rTwo/2) -- (\aOneTwo+180:\rTwo);
\draw[-latex, very thick, shift={(B)}] (\aTwoThree:\rTwo/2) -- (\aTwoThree:\rTwo);
\draw[-latex, very thick, shift={(C)}] (\aTwoThree+180:\rThree/2) -- (\aTwoThree+180:\rThree);
\end{tikzpicture}
\end{document}
输出
编辑2:只是为了好玩,下面是用它制作的一个小动画。生成后.pdf
,它使用 ImageMagick 对其进行转换:
convert -loop 0 -delay 2 -density 250 -dispose previous richard.pdf gear.gif
代码
\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{arrows, calc}
\newcommand{\gear}[5]{%
\foreach \i in {1,...,#1}
{ [rotate=(\i-1)*360/#1] (0:#2) arc (0:#4:#2) {[rounded corners=0.5pt] -- (#4+#5:#3) arc (#4+#5:360/#1-#5:#3)} -- (360/#1:#2)
}
}
\begin{document}
% given
\pgfmathsetmacro{\rOne}{1.6}% inner radius
\pgfmathsetmacro{\nOne}{17}% num teeth
\pgfmathsetmacro{\nTwo}{23}
\pgfmathsetmacro{\nThree}{19}
\pgfmathsetmacro{\toothHeight}{0.2}
\pgfmathsetmacro{\toothFall}{1}% degrees where radius drops from inner to outer
\pgfmathsetmacro{\aOneTwo}{120}% angle from first to second
\pgfmathsetmacro{\aTwoThree}{20}% angle from first to second
\pgfmathtruncatemacro{\numFrames}{50}% number of individual pictures
% computed
\pgfmathsetmacro{\rTwo}{\rOne*\nTwo/\nOne}% via equating tooth lengths
\pgfmathsetmacro{\rThree}{\rOne*\nThree/\nOne}%
\pgfmathsetmacro{\ROne}{\rOne+\toothHeight}% outer radius
\pgfmathsetmacro{\RTwo}{\rTwo+\toothHeight}
\pgfmathsetmacro{\RThree}{\rThree+\toothHeight}
\pgfmathsetmacro{\dOne}{(360/\nOne-\toothFall)/2}% degrees for a tooth; here inner deg = outer deg
\pgfmathsetmacro{\dTwo}{(360/\nTwo-\toothFall)/2}
\pgfmathsetmacro{\dThree}{(360/\nThree-\toothFall)/2}
\pgfmathsetmacro{\distOneTwo}{\rOne+\rTwo+\toothHeight+0.05}
\pgfmathsetmacro{\distTwoThree}{\rTwo+\rThree+\toothHeight+0.05}
\pgfmathsetmacro{\rotOne}{(360/\nOne/\numFrames}
\pgfmathsetmacro{\rotTwo}{(360/\nTwo/\numFrames}
\pgfmathsetmacro{\rotThree}{(360/\nThree/\numFrames}
\foreach \frame in {1,...,\numFrames}
{ \begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at ($(A)+(\aOneTwo:\distOneTwo)$);
\coordinate (C) at ($(B)+(\aTwoThree:\distTwoThree)$);
\draw[white] (-4.5,-1.9) rectangle (4,7);
\draw[thick, rotate=9+\frame*\rotOne] node[align=center] {$\alpha$ \\ \nOne\ Teeth} \gear{\nOne}{\rOne}{\ROne}{\dOne}{\toothFall} ;
\draw[thick, shift={(B)}, rotate=7-\frame*\rotTwo] node[align=center] {$\beta$ \\ \nTwo\ Teeth} \gear{\nTwo}{\rTwo}{\RTwo}{\dTwo}{\toothFall};
\draw[thick, shift={(C)}, rotate=8+\frame*\rotThree] node[align=center] {$\gamma$ \\ \nThree\ Teeth} \gear{\nThree}{\rThree}{\RThree}{\dThree}{\toothFall};
\draw[-latex, very thick] (\aOneTwo:\rOne/2) -- (\aOneTwo:\rOne);
\draw[-latex, very thick, shift={(B)}] (\aOneTwo+180:\rTwo/2) -- (\aOneTwo+180:\rTwo);
\draw[-latex, very thick, shift={(B)}] (\aTwoThree:\rTwo/2) -- (\aTwoThree:\rTwo);
\draw[-latex, very thick, shift={(C)}] (\aTwoThree+180:\rThree/2) -- (\aTwoThree+180:\rThree);
\end{tikzpicture}
}
\end{document}