tikz - fillinbetween 不如预期

tikz - fillinbetween 不如预期

我想在齿轮和圆圈之间进行填充,但它却填充了整个齿轮!

\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}

在此处输入图片描述

答案1

你不需要fillbetween那个,even odd rule这就足够了。

\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}

在此处输入图片描述

答案2

稍微改变一下实现,供参考:

\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}

相关内容