我正在尝试在 TikZ 中创建一个齿轮,其中可以确定尖刺的数量和大小。我浏览了 pgfmanual,偶然发现了装饰部分,然后在那里尝试了一段代码。以下是我目前所拥有的:
\documentclass[letterpaper]{article}
\usepackage{tikz}
\usetikzlibrary{decorations}
\begin{document}
\pgfdeclaredecoration{example}{initial}
{
\state{initial}[width=20pt]
{
\pgfpathlineto{\pgfpoint{0pt}{5pt}}
\pgfpathlineto{\pgfpoint{5pt}{5pt}}
\pgfpathlineto{\pgfpoint{5pt}{-5pt}}
\pgfpathlineto{\pgfpoint{10pt}{-5pt}}
\pgfpathlineto{\pgfpoint{10pt}{5pt}}
}
\state{final}
{
\pgfpathlineto{\pgfpointdecoratedpathlast}
}
}
\tikz[decoration=example]
{
\draw [red,decorate] (0,0) circle (1cm);
}
\end{document}
得出的结果是:
当然,以上方法并不令人满意。有没有办法在 TikZ 或任何其他图形包中绘制齿轮?
答案1
您需要调整唯一的线条来绘制图片。此处的旋转是 360/10。可以修改弧的长度、齿的类型。
\documentclass[11pt]{scrartcl}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[thick]
\foreach \i in {1,2,...,10} {%
[rotate=(\i-1)*36]
(0:2) arc (0:18:2) {[rounded corners=2pt] -- ++(18: 0.3) arc (18:36:2.3) } -- ++(36: -0.3)
};
\end{tikzpicture}
\end{document}
更新
如何修改:
齿数:n
半径内圆 r1
外圆半径 r2
旋转 = 360/n
第一个圆弧起始角度=0结束角度=
arc(0:a:r1)
第二圆弧起始角度=a+b终止角度=<360/nb>
arc(a+b:360/n-b:r2)
圆弧之间的线
-- (a+b:r2)
最后一行
-- (360/n:r1)
\begin{tikzpicture} \draw[thick] \foreach \i in {1,2,...,10} {% [rotate=(\i-1)*36] (0:2) arc (0:12:2) -- (18:2.4) arc (18:30:2.4) -- (36:2) }; \end{tikzpicture}
使用宏
\documentclass[11pt]{scrartcl}
\usepackage{tikz}
% #1 number of teeths
% #2 radius intern
% #3 radius extern
% #4 angle from start to end of the first arc
% #5 angle to decale the second arc from the first
\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[thick] \gear{18}{2}{2.4}{10}{2};
\end{tikzpicture}
\end{document}
使用调整后的宏
...您还可以使用\draw[fill]
\documentclass[11pt]{scrartcl}
\usepackage{tikz}
% #1 number of teeths
% #2 radius intern
% #3 radius extern
% #4 angle from start to end of the first arc
% #5 angle to decale the second arc from the first
% #6 inner radius to cut off
\newcommand{\gear}[6]{%
(0:#2)
\foreach \i [evaluate=\i as \n using {\i-1)*360/#1}] in {1,...,#1}{%
arc (\n:\n+#4:#2) {[rounded corners=1.5pt] -- (\n+#4+#5:#3)
arc (\n+#4+#5:\n+360/#1-#5:#3)} -- (\n+360/#1:#2)
}%
(0,0) circle[radius=#6]
}
\begin{document}
\begin{tikzpicture}
\fill[even odd rule] \gear{18}{2}{2.4}{10}{2}{1};
\end{tikzpicture}
\end{document}
答案2
这是一个更符合表示牙齿圆的方法,通过展开圆,可以精确地确定牙齿的名称、形状和压力角,从而获得更简单的追踪头和脚的圆的轨迹
这里有一个更符合渐开线齿轮表示的解决方案,它指定齿数、模数、压力角以获得布局,我仍然简化了圆头和圆脚的图
注意:此处的脚本不支持 PGF 2.1 或更高版本的 pgf 3
警告:第一个脚本只能使用 pgf2.1 运行
\documentclass[11pt]{scrartcl}
\usepackage{tikz}
\newcommand{\gear}[3]{%
\def\modu{#1}
\def\Zb{#2}
\def\AngleA{#3}
\pgfmathsetmacro{\Rpr}{\Zb*\modu/2}
\pgfmathsetmacro{\Rb}{\Rpr*cos(\AngleA)}
\pgfmathsetmacro{\Rt}{\Rpr+\modu}
\pgfmathsetmacro{\Rp}{\Rpr-1.25*\modu}
\pgfmathsetmacro{\AngleT}{pi/180*acos(\Rb/\Rt)}
\pgfmathsetmacro{\AnglePr}{pi/180*acos(\Rb/\Rpr)}
\pgfmathsetmacro{\demiAngle}{180/\Zb}
\pgfmathsetmacro{\Angledecal}{(\demiAngle-2*\AnglePr)/2}
% \draw[dashed] (0,0) circle (\Rpr);
% \draw[red,dashed] (0,0) circle (\Rb);
% \draw[dashed] (0,0) circle (\Rt);
% \draw[dashed] (0,0) circle (\Rp);
\foreach \zz in{1,2,...,\Zb}{
\coordinate(e\zz) at (\zz/\Zb*360+\Angledecal:\Rb);
\draw[domain=-0:\AngleT,smooth,variable=\t,thick]
plot ({{180/pi*(-\t+tan(180/pi*\t)) +\zz/\Zb*360+\Angledecal}:\Rb/cos(180/pi*\t)})coordinate(d\zz);
\coordinate(g\zz) at ({(\zz))/\Zb*360-\Angledecal}:\Rb);
\draw[domain=-0:-\AngleT,smooth,variable=\t,thick]
plot ({{180/pi*(-\t+tan(180/pi*\t)) +(\zz+1)/\Zb*360-\Angledecal}:\Rb/cos(180/pi*\t)})coordinate(f\zz);
\draw[blue] (d\zz) to[bend right=\demiAngle] (f\zz);
\draw[rounded corners=\modu](e\zz) -- (\zz/\Zb*360+\Angledecal:\Rp) to[bend left=\demiAngle] (\zz/\Zb*360-\Angledecal:\Rp) -- (g\zz);
}
}
\begin{document}
\begin{tikzpicture}[scale=0.2]
\gear{3}{15}{20}
\begin{scope}[xshift=40.5cm,rotate=180/12]
\gear{3}{12}{20}
\end{scope}
\end{tikzpicture}
\end{document}
适用于 PGF3 的脚本
\newcommand{\gear}[4][]{%
\def\modu{#2}
\def\Zb{#3}
\def\AngleA{#4}
\pgfmathsetmacro{\Rpr}{\Zb*\modu/2}
\pgfmathsetmacro{\Rb}{\Rpr*cos(\AngleA)}
\pgfmathsetmacro{\Rt}{\Rpr+\modu}
\pgfmathsetmacro{\Rp}{\Rpr-1.25*\modu}
\pgfmathsetmacro{\AngleT}{sqrt(\Rt^2/\Rb^2-1)}
%\pgfmathsetmacro{\AnglePr}{pi/180*acos(\Rb/\Rpr)}
\pgfmathsetmacro{\AnglePr}{180/pi*sqrt(\Rpr^2/\Rb^2-1)}
\pgfmathsetmacro{\demiAngle}{180/\Zb}
\pgfmathsetmacro{\Angledecal}{(\demiAngle+0.075*\AnglePr)/2}%
\def\xxt{\Rb*(cos(\t r)+\t*sin(\t r))}
\def\yyt{\Rb*(sin(\t r) - \t*cos(\t r))}
\foreach \zz in{1,2,...,\Zb}{
\coordinate(e\zz) at (\zz/\Zb*360+\Angledecal:\Rb);
\draw[fill](e\zz)circle(0.1);
\draw[domain=-0:\AngleT,smooth,variable=\t,thick,green,#1]
plot ({atan2(\xxt,\yyt)-90+\zz/\Zb*360+\Angledecal}:{\Rb*sqrt(1+\t^2)} )coordinate(f\zz);
\coordinate(g\zz) at ({(\zz+1))/\Zb*360-\Angledecal}:\Rb);
\draw[domain=-0:\AngleT,smooth,variable=\t,thick,green,#1]
plot ({atan2(\xxt,-\yyt)-90+(\zz)/\Zb*360-\Angledecal}:{\Rb*sqrt(1+\t^2)} )coordinate(d\zz);
\draw[#1] (f\zz) to[bend left=\demiAngle] (d\zz);
\draw[rounded corners=\modu,#1](e\zz) -- (\zz/\Zb*360+\Angledecal:\Rp) to[bend right=\demiAngle] ({(\zz+1)/\Zb*360-\Angledecal}:\Rp) -- (g\zz);
}
}
\begin{tikzpicture}[scale=0.15]
\gear[red,ultra thick]{2}{15}{20}
\draw[red,ultra thick] (0,0) coordinate(O1)node[below left]{$O_1$}circle (1);
\draw[thin,red,dashed](O1) circle (15);
\begin{scope}[xshift=27cm,rotate=180/12]
\gear[blue,ultra thick]{2}{12}{20}
\draw[blue,ultra thick] (0,0)coordinate(O2)node[below left]{$O_2$} circle (1);
\draw[thin,blue,dashed](O2) circle (12);
\end{scope}
\draw[thin,black,dashed] (O1) --++(-18,0) --(15,0) coordinate(I) node[below right]{$I$}-- (O2) --++(18,0);
\draw[thin,black,dashed] (O1)--+(0,18)--+(0,-18);
\draw[thin,black,dashed] (O2)--+(0,18)--+(0,-18);
\draw[thin,black] (I) -- +(70:15)--+(-110:15);
\draw[thin,black] (I) --+(70:12)coordinate(aa)-- +(-70:15)--+(110:15);
\draw[thin,black] (I) -- +(-90:15)--+(90:12)coordinate(bb)--+(90:15);
\draw[-latex] (bb) to [bend left=10] node[above]{$\alpha$} (aa);
\draw (d1) -- ++(1,1);
\end{tikzpicture}
答案3
最简单的方法可能是为\foreach
每个齿重复精确计算的路径。可能类似于以下内容(虽然我不知道齿轮的形状到底如何才能正常工作)。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\def\teeth{10}
\def\innerRadius{1cm}
\def\outerRadius{1.3cm}
\pgfmathsetmacro\angle{360/(2*\teeth)}
\foreach \i in {1,2,...,\teeth} {
\draw ({\i*\angle*2}:\innerRadius)
-- ({(2*\i+0.5)*\angle}:\outerRadius)
arc [radius=\outerRadius, start angle={(2*\i+0.5)*\angle}, end angle={(2*\i+.9)*\angle}]
-- ({(2*\i+1.4)*\angle}:\innerRadius)
arc [radius=\innerRadius, start angle={(2*\i+1.4)*\angle}, end angle={(2*\i+2)*\angle}];
}
\end{tikzpicture}
\end{document}
答案4
Paul Gaborit 的解决方案提到无法在 a 内部使用plot
。foreach
这\path
似乎已经改变,所以这里有一个经过调整的解决方案,即一个封闭的形状。此外,我们现在有了pic
s!
\documentclass[tikz,margin=10pt]{standalone}
\tikzset{pics/gear/.style n args={3}{
code={
\def\modu{#1}
\def\Zb{#2}
\def\AngleA{#3}
\pgfmathsetmacro{\Rpr}{\Zb*\modu/2}
\pgfmathsetmacro{\Rb}{\Rpr*cos(\AngleA)}
\pgfmathsetmacro{\Rt}{\Rpr+\modu}
\pgfmathsetmacro{\Rp}{\Rpr-1.25*\modu}
\pgfmathsetmacro{\AngleT}{pi/180*acos(\Rb/\Rt)}
\pgfmathsetmacro{\AnglePr}{pi/180*acos(\Rb/\Rpr)}
\pgfmathsetmacro{\demiAngle}{180/\Zb}
\pgfmathsetmacro{\Angledecal}{(\demiAngle-2*\AnglePr)/2}
\path[pic actions] foreach \zz in{1,...,\Zb}{
\ifnum\zz=1
% don't use a lineto in the first iteration
(\zz/\Zb*360-\Angledecal:\Rp)
\else
-- (\zz/\Zb*360-\Angledecal:\Rp)
\fi
to[bend right=\demiAngle]
(\zz/\Zb*360+\Angledecal:\Rp)
--
plot[domain=-0:\AngleT,smooth,variable=\t]
({{180/pi*(-\t+tan(180/pi*\t)) +\zz/\Zb*360+\Angledecal}:\Rb/cos(180/pi*\t)})
%
to[bend right=\demiAngle]
({{180/pi*(\AngleT+tan(180/pi*-\AngleT)) +(\zz+1)/\Zb*360-\Angledecal}:
\Rb/cos(180/pi*-\AngleT)})
--
plot[domain=-\AngleT:-0,smooth,variable=\t]
({{180/pi*(-\t+tan(180/pi*\t)) +(\zz+1)/\Zb*360-\Angledecal}:\Rb/cos(180/pi*\t)})
} -- cycle;
}
}}
\begin{document}
\begin{tikzpicture}
% observations:
%
% - param #1 and #3 must be equal for gears to mesh
% - the required distance is (#2_1 + #2_2) * #1 / 2
% - for odd numbers of teeth, gears on a horizontal axis fit without rotation
\pic[draw,fill=red!20!white] at (0,0) {gear={0.50}{17}{15}};
\pic[draw,fill=red!20!white] at (6,0) {gear={0.50}{ 7}{15}};
\pic[draw,fill=blue!20!white,rotate=-60 + 90/11] at (0,0) {gear={0.25}{11}{20}};
\pic[draw,fill=blue!20!white,rotate=-60 - 90/29] at (-60:5) {gear={0.25}{29}{20}};
\foreach \p in {(0,0),(6,0),(-60:5)} \fill \p circle (3pt);
\end{tikzpicture}
\end{document}
\ifnum
欢迎提出任何关于如何消除丑陋的建议。