2

I would like to fill in between the gear and circle but it filled the whole gear instead!

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc,pgfplots.fillbetween}
\begin{document}
\begin{tikzpicture}
    \def\ra{2}
    \def\rb{2.5}
    \def\n{8}
    \foreach \i in {1,...,\n} {
        \coordinate (P\i) at (\i*360/\n:\ra);
        \coordinate (Q\i) at (\i*360/\n + 180/\n:\ra);
        \coordinate (R\i) at (\i*360/\n + 180/\n + 90/\n:\rb);
        \coordinate (S\i) at (\i*360/\n+360/\n:\ra);
        \path (R\i) -- +($(Q\i)-(S\i)$) coordinate[pos=0.5](U\i);
        \path (R\i) -- +($(S\i)-(Q\i)$) coordinate[pos=0.5](V\i);
        %\foreach \x in {P,Q,R,S,U,V} {
        %   \node[circle,fill=red,inner sep=1,label=\i] at (\x\i) {};
        %}
    }
    \draw[name path = A] (360/\n:\ra) foreach \i in {1,...,\n} {
        -- (P\i) arc (\i*360/\n:(\i+0.5)*360/\n:\ra) -- (U\i) -- (V\i) -- (S\i)
    } -- cycle; 
    \draw[name path = B] (0,0) circle (\ra/2);  
    \tikzfillbetween[of=A and B]{gray, opacity=0.2};
    \end{tikzpicture}
\end{document}

enter image description here

7

You do not need fillbetween for that, even odd rule is sufficient.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
    \def\ra{2}
    \def\rb{2.5}
    \def\n{8}
    \foreach \i in {1,...,\n} {
        \coordinate (P\i) at (\i*360/\n:\ra);
        \coordinate (Q\i) at (\i*360/\n + 180/\n:\ra);
        \coordinate (R\i) at (\i*360/\n + 180/\n + 90/\n:\rb);
        \coordinate (S\i) at (\i*360/\n+360/\n:\ra);
        \path (R\i) -- +($(Q\i)-(S\i)$) coordinate[pos=0.5](U\i);
        \path (R\i) -- +($(S\i)-(Q\i)$) coordinate[pos=0.5](V\i);
    }
    \draw[fill=gray,fill opacity=0.2,even odd rule] (360/\n:\ra) foreach \i in {1,...,\n} {
        -- (P\i) arc (\i*360/\n:(\i+0.5)*360/\n:\ra) -- (U\i) -- (V\i) -- (S\i)
    } -- cycle
     (0,0) circle[radius=\ra/2];  
\end{tikzpicture}
\end{document}

enter image description here

0

Change the implementation a little bit for reference:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
    \def\ra{2}
    \def\rb{2.5}
    \def\n{8}
    \def\a{360/\n}
    \draw[fill=gray!30,even odd rule] (\a:\ra) 
        foreach \i in {1,...,\n} {
            -- (\i*\a:\ra) arc (\i*\a:(\i+0.5)*\a:\ra) -- 
            ++({(\i+0.75)*\a}:\rb-\ra) -- 
            ++($({(\i+1)*\a}:\ra) - ({(\i+0.5)*\a}:\ra)$) coordinate (P\i)
        } -- cycle  
    (0,0) circle (\ra/2); 
    \end{tikzpicture}
\end{document}

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.