3

I am looking for a simple code in TikZ drawing two gears in mesh so that the cogs of each gear are made from a sinusoidal function wrapped around a circle. The bigger gear is to be colored yellow and have 50 cogs, the smaller gear is to be colored red and have 10 cogs. The cogs of both gears are to have the same amplitude, which is a small fraction - maybe an eighth - of the radius of the smaller gear. The smaller gear is to sit atop the bigger gear, with a well of the smaller gear aligned with a cog of the bigger gear.

I do not have code for this. The closest code that I found for what I want is at the following web site. Creating gears in TikZ It is not of any help, though.

I have no code for wrapping 10 periods of a sinusoidal function around a circle.

8

This defines a pic for the gear. It takes the parameters

  • the number of cogs n,
  • the radius of the inner circle r1,
  • the radius of the gear r2,
  • the amplitude a.

The other things like draw color, fill color and transformations like rotations can be passed to the options of the pic. I also added the ratio \Ratio of cogs.

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[pics/sinus gear/.style={code={
   \tikzset{gear/.cd,#1}
   \def\pv##1{\pgfkeysvalueof{/tikz/gear/##1}}
   \pgfmathtruncatemacro{\mymod}{360/\pv{n}}
   \draw[pic actions] 
    plot[variable=\t,domain=0:360,samples=12*\pv{n}+1,smooth cycle] 
    (\t:{\pv{r2}+\pv{a}*sin(\pv{n}*Mod(\t,\mymod))})
     (0,0) circle[radius=\pv{r1}];
    }},
    gear/.cd,r1/.initial=0.8cm,r2/.initial=1cm,n/.initial=12,a/.initial=0.1cm]
   \begin{scope}[local bounding box=gears]
    \def\Ratio{5}
    \path (0,0) pic[fill=orange!20,draw=orange,even odd rule]
        {sinus gear={n=10*\Ratio,r1=\Ratio*0.3cm,r2=\Ratio*0.5cm}} 
        (135:{(1+\Ratio)*0.5cm+\pgflinewidth}) 
        pic[fill=red!20,even odd rule,draw=red,rotate=90/\Ratio]{sinus
        gear={n=10,r1=0.3cm,r2=0.5cm}};
   \end{scope}  
\end{tikzpicture}
\end{document}

enter image description here

This is a clipped version with different colors and inner radii (and an arrow). The reason why one may want to clip it is that then cogs match. If you want a cog up or down, rotate the gear by integer multiples of 90/n, where n is the number of cogs.

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[pics/sinus gear/.style={code={
   \tikzset{gear/.cd,#1}
   \def\pv##1{\pgfkeysvalueof{/tikz/gear/##1}}
   \pgfmathtruncatemacro{\mymod}{360/\pv{n}}
   \clip plot[variable=\t,domain=0:360,samples=12*\pv{n}+1,smooth cycle] 
    (\t:{\pv{r2}+\pv{a}*sin(\pv{n}*Mod(\t,\mymod))})
     (0,0) circle[radius=\pv{r1}];
   \draw[pic actions] 
    plot[variable=\t,domain=0:360,samples=12*\pv{n}+1,smooth cycle] 
    (\t:{\pv{r2}+\pv{a}*sin(\pv{n}*Mod(\t,\mymod))})
     (0,0) circle[radius=\pv{r1}];
    }},
    gear/.cd,r1/.initial=0.8cm,r2/.initial=1cm,n/.initial=12,a/.initial=0.1cm]
   \begin{scope}[local bounding box=gears]
    \def\Ratio{5}
    \path (0,0) pic[fill=yellow,draw=orange,ultra thick,even odd rule,rotate=90/50]
        {sinus gear={n=10*\Ratio,r1=\Ratio*0.3cm,r2=\Ratio*0.5cm}} 
        (90:{(1+\Ratio)*0.5cm}) 
        pic[fill=blue,even odd rule,draw=purple,ultra thick,
            rotate={90*(\Ratio+1)+\Ratio*90/50+18}]{sinus gear={n=10,r1=0.3cm,r2=0.5cm}};
    \draw[-latex,] (90:{(1+\Ratio)*0.5cm+\pgflinewidth}) 
    ++ (0,-0.8*0.3cm) -- ++(0,1.6*0.3cm);
   \end{scope}  
\end{tikzpicture}
\end{document}

enter image description here

An animated version.

\documentclass[tikz,border=3mm]{standalone}
\tikzset{pics/sinus gear/.style={code={
   \tikzset{gear/.cd,#1}
   \def\pv##1{\pgfkeysvalueof{/tikz/gear/##1}}
   \pgfmathtruncatemacro{\mymod}{360/\pv{n}}
   \draw[pic actions] 
    plot[variable=\t,domain=0:360,samples=12*\pv{n}+1,smooth cycle] 
    (\t:{\pv{r2}+\pv{a}*sin(\pv{n}*Mod(\t,\mymod))})
     (0,0) circle[radius=\pv{r1}];
    }},
    gear/.cd,r1/.initial=0.8cm,r2/.initial=1cm,n/.initial=12,a/.initial=0.1cm}
\begin{document}
\foreach \X in {1,...,36}
{\begin{tikzpicture}
    \path[use as bounding box] (-3.1,-2.6) rectangle (2.6,3.1);
    \def\Ratio{5}
    \path (0,0) pic[fill=orange!20,draw=orange,even odd rule,rotate=\X/\Ratio]
        {sinus gear={n=10*\Ratio,r1=\Ratio*0.3cm,r2=\Ratio*0.5cm}} 
        (135:{(1+\Ratio)*0.5cm+\pgflinewidth}) 
        pic[fill=red!20,even odd rule,draw=red,rotate=90/\Ratio-\X]{sinus
        gear={n=10,r1=0.3cm,r2=0.5cm}};  
\end{tikzpicture}}
\end{document}

enter image description here

\documentclass[tikz,border=3mm]{standalone}
\tikzset{pics/sinus gear/.style={code={
   \tikzset{gear/.cd,#1}
   \def\pv##1{\pgfkeysvalueof{/tikz/gear/##1}}
   \pgfmathtruncatemacro{\mymod}{360/\pv{n}}
   \draw[pic actions] 
    plot[variable=\t,domain=0:360,samples=12*\pv{n}+1,smooth cycle] 
    (\t:{\pv{r2}+\pv{a}*sin(\pv{n}*Mod(\t,\mymod))})
     (0,0) circle[radius=\pv{r1}];
    }},
    gear/.cd,r1/.initial=0.8cm,r2/.initial=1cm,n/.initial=12,a/.initial=0.1cm}
\newsavebox\BigGear
\newsavebox\SmallGear
\def\Ratio{5}
\sbox\BigGear{\tikz{\pic[fill=orange!20,draw=orange,even odd rule]
        {sinus gear={n=10*\Ratio,r1=\Ratio*0.3cm,r2=\Ratio*0.5cm}};}}
\sbox\SmallGear{\tikz{\pic[fill=red!20,even odd rule,draw=red]{sinus
        gear={n=10,r1=0.3cm,r2=0.5cm}};}}
\begin{document}
\foreach \X in {1,...,90}
{\begin{tikzpicture}
    \path[use as bounding box] (-3.4,-3.4) rectangle (3.4,3.4);
    \path (0,0) node{\usebox\BigGear}
        (4*\X:{(1+\Ratio)*0.5cm+\pgflinewidth}) 
        node[rotate={4*(\Ratio+1)*\X}]{\usebox\SmallGear};
\end{tikzpicture}}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.