【TikZ】塗りつぶし fill

塗りつぶしfill

TeXclip○ obsidian○

\drawと同じ要領で,\fillを使うと塗りつぶしができます。

\begin{tikzpicture}
        \fill[blue] (0,0) circle (2cm);
        \fill[blue,xshift=2.5cm,yshift=-2cm] (0,0) -- (4,0) -- (4,4) -- (0,4) -- cycle;
\end{tikzpicture}

閉じた曲線の内側を塗りつぶします。色の指定をしない場合は,黒で塗られます。

閉じていない線を書いた場合,始点と終点が結ばれて閉曲線になります。

\begin{tikzpicture}
    \fill[blue] (0,0) -- (5,0) -- (4,3);%閉じていない
\end{tikzpicture}

オプションにdrawをつけると,縁に線をかきます。

\begin{tikzpicture}
    \fill[cyan,draw=black,very thick] (0,0) circle (2cm);
\end{tikzpicture}

線が閉じていない場合は次のようになります。

\begin{tikzpicture}
        \fill[cyan,draw=black,very thick] (0,0) -- (5,0) -- (4,3);%閉じていない
\end{tikzpicture}

閉曲線が複数ある場合の塗りつぶしのルール

\fillに複数の曲線を描いた場合に,領域を塗りつぶすルールが2つあります。デフォルトとeven odd ruleオプションです。両方のオプションは,ともにwinding number(巻き数)で塗りつぶすかどうかを判断しています。このルールはpatternオプションにも適応されます。

巻き数は,大雑把に言えば,その領域が何回閉曲線で囲われたかを表す数です。

2つの例を挙げます。左がデフォルト,右がeven odd ruleオプションです。

\begin{tikzpicture}
    \fill[blue] (0,0) -- (2,0) -- (2,2) -- (0,2) -- cycle (2,2) circle (0.8cm);
    \fill[blue,even odd rule,xshift=5cm] (0,0) -- (2,0) -- (2,2) -- (0,2) -- cycle (2,2) circle (0.8cm);
\end{tikzpicture}

デフォルトは,マニュアルではthe nonzero winding number ruleと呼ばれていて,巻き数が0の領域は塗りつぶさず,それ以外で塗りつぶします。even odd ruleオプションでは,巻き数が偶数では塗りつぶさず,奇数のところを塗りつぶします。

the nonzero winding number rule

下の例は,同じ形の図形をデフォルトで塗りつぶしたものです。違いは,長方形を書くときの領域の囲い方です。左が反時計回り,右が時計回りです。

\begin{tikzpicture}
    \fill[blue] (0,0) -- (2,0) -- (2,2) -- (0,2) -- cycle (2,2) circle (0.8cm);
    \fill[blue,xshift=5cm] (0,0) -- (0,2) -- (2,2) -- (2,0) -- cycle (2,2) circle (0.8cm);
\end{tikzpicture}

この右傾の巻き数は下のようになっています。

texclip.marutank.net

反時計回りで囲うと巻き数は+1,時計回りで囲うと巻数は-1されます。左の図形では,長方形,円ともに反時計回りで,2つの領域が重なる部分では$+1+1=+2$になります。一方,右の図形では,長方形が時計回りになっているので,巻き数が-1になり,円と長方形が重なる領域では$+1-1=0$となります。巻き数0の場合は塗りつぶされません。

the even odd rule

次は,上と同じものをeven odd ruleで塗りつぶしたものです。

\begin{tikzpicture}
    \fill[blue,even odd rule] (0,0) -- (2,0) -- (2,2) -- (0,2) -- cycle (2,2) circle (0.8cm);
    \fill[blue,even odd rule,xshift=5cm] (0,0) -- (0,2) -- (2,2) -- (2,0) -- cycle (2,2) circle (0.8cm);
\end{tikzpicture}

texclip.marutank.net

巻き数が偶数の2,0の領域は塗られていません。一方で,奇数の-1,1の部分が塗られていることが分かります。

デフォルトのルールでrectanglecircleを重ねると次のようになります。rectanleは時計回りの囲い方になっています。

\begin{tikzpicture}
    \fill[blue] (0,0) rectangle (3,4) (2,2) circle (0.8cm);
\end{tikzpicture}

even odd ruleを使えば,円を2つかいてドーナツの形に塗ることもできます。

\begin{tikzpicture}
    \fill[postaction={draw=black},blue] (2,2) circle (2cm) -- (2,2) circle (0.5cm);
    \fill[even odd rule,blue,xshift=5cm] (2,2) circle (2cm) -- (2,2) circle (0.5cm);
\end{tikzpicture}

星型(五芒星)

\usetikzlibrary{math}
\begin{tikzpicture}
\tikzmath{
    coordinate \A;
    \A = ({3*cos(90-72)},{3*sin(90-72)});
    coordinate \B;
    \B = ({3*cos(90)},{3*sin(90)});
    coordinate \C;
    \C = ({3*cos(90+72)},{3*sin(90+72)});
    coordinate \D;
    \D = ({3*cos(90+144)},{3*sin(90+144)});
    coordinate \E;
    \E = ({3*cos(90+216)},{3*sin(90+216)});
}
\fill[blue,even odd rule] (\A) -- (\C) -- (\E) -- (\B) -- (\D) -- cycle;

%\draw[thick] (\A) -- (\C) -- (\E) -- (\B) -- (\D) -- cycle;
\end{tikzpicture}

texclip.marutank.net

\clipのルール

TeXclip○ obsidian○

clipの場合,巻き数がゼロでない領域がくり抜かれる,the nonzero winding number ruleに従います。even odd ruleはありません。

四角形の内側を青で塗りつぶしています。

\begin{tikzpicture}
\begin{scope}
\clip (0,0) -- (4,0) -- (4,4) -- (0,4) -- cycle (4,4) circle (2cm);
\fill[blue] (-1,-1) rectangle (7,7);
\end{scope}
\draw[thick] (-1,-1) rectangle (7,7);
\end{tikzpicture}
\begin{tikzpicture}
\begin{scope}
\clip (0,0) -- (0,4) -- (4,4) -- (4,0) -- cycle (4,4) circle (2cm);
\fill[blue] (-1,-1) rectangle (7,7);
\end{scope}
\draw[thick] (-1,-1) rectangle (7,7);
\end{tikzpicture}

TikZでベン図をかく

※patternsライブラリを使用しています。 TeXclip○ obsidian×(patternsライブラリを使用したもの)

下のようなベン図をかくには工夫が必要です。\clipを使わない場合は,白で塗る工程を入れます。背景を入れたい場合は\clipを使った方がいいです。

白の塗りつぶしを入れたもの。

\usetikzlibrary{patterns}
\begin{tikzpicture}
\fill[pattern= north west lines] (0,0) circle (2cm) (2,0) circle (1.8cm);
\fill[even odd rule,white] (0,0) circle (2cm) (2,0) circle (1.8cm);
\draw[thick] (0,0) circle (2cm) (2,0) circle (1.8cm);
\end{tikzpicture}

\clipを使ったもの。

\usetikzlibrary{patterns}
\begin{tikzpicture}

\begin{scope}
    \clip (2,0) circle (1.8cm);
    \fill[pattern= north west lines] (0,0) circle (2cm);
\end{scope}

\draw[thick] (0,0) circle (2cm) (2,0) circle (1.8cm);
\end{tikzpicture}
\usetikzlibrary{patterns}
\begin{tikzpicture}
\fill[even odd rule,pattern=north west lines] (0,0) circle (2cm) (2,0) circle (1.8cm);
\draw[thick] (0,0) circle (2cm) (2,0) circle (1.8cm);
\end{tikzpicture}
\usetikzlibrary{patterns}
\begin{tikzpicture}
\begin{scope}
\clip (2,0) circle (1.8cm);
\fill[pattern=north west lines,even odd rule] (0,0) circle (2cm) (2,0) circle (1.8cm);
\end{scope}

\draw[thick] (0,0) circle (2cm) (2,0) circle (1.8cm);
\end{tikzpicture}
\usetikzlibrary{patterns}
\begin{tikzpicture}
\begin{scope}
\clip (-3,-3) -- (-3,3) -- (5,3) -- (5,-3) -- cycle (0,0) circle (2cm);
\clip (-3,-3) -- (-3,3) -- (5,3) -- (5,-3) -- cycle (2,0) circle (1.8cm);
\fill[pattern=north west lines,even odd rule] (-3,-3) -- (5,-3) -- (5,3) -- (-3,3) -- cycle;
\end{scope}

\draw[thick] (0,0) circle (2cm) (2,0) circle (1.8cm);
\draw[thick] (-3,-3) -- (5,-3) -- (5,3) -- (-3,3) -- cycle;
\end{tikzpicture}
\usetikzlibrary{math,patterns}
\begin{tikzpicture}
\tikzmath{
    \radius = 2;
}
\begin{scope}
\clip plot[samples=50,domain=360:0] ({\radius*(cos(\x)+cos(60))},{\radius*(sin(\x)+sin(60))});
\clip plot[samples=50,domain=360:0] ({\radius*(cos(\x)+cos(0))},{\radius*(sin(\x)+sin(0))});

\fill[pattern=north west lines,even odd rule] plot[samples=50,domain=360:0] ({\radius*cos(\x)},{\radius*sin(\x)})
plot[samples=50,domain=360:0] ({\radius*(cos(\x)+cos(0))},{\radius*(sin(\x)+sin(0))});
\end{scope}

\draw[thick,samples=50,smooth,domain=0:360] plot ({\radius*(cos(\x)+cos(60))},{\radius*(sin(\x)+sin(60))});
\draw[thick,samples=50,smooth,domain=0:360] plot ({\radius*cos(\x)},{\radius*sin(\x)});
\draw[thick,samples=50,smooth,domain=0:360] plot ({\radius*(cos(\x)+cos(0))},{\radius*(sin(\x)+sin(0))});
\end{tikzpicture}
\usetikzlibrary{math,patterns}
\begin{tikzpicture}
\tikzmath{
    \radius = 2;
}
\begin{scope}
\clip plot[samples=50,domain=0:360] ({\radius*(cos(\x)+cos(60))},{\radius*(sin(\x)+sin(60))}) plot[samples=50,domain=360:0] ({\radius*(cos(\x)+cos(0))},{\radius*(sin(\x)+sin(0))});
\clip plot[samples=50,domain=0:360] ({\radius*(cos(\x)+cos(60))},{\radius*(sin(\x)+sin(60))}) plot[samples=50,domain=360:0] ({\radius*cos(\x)},{\radius*sin(\x)});
\fill[pattern=north west lines] plot[samples=50,domain=0:360] ({\radius*(cos(\x)+cos(60))},{\radius*(sin(\x)+sin(60))});
\end{scope}

\draw[thick,samples=50,smooth,domain=0:360] plot ({\radius*(cos(\x)+cos(60))},{\radius*(sin(\x)+sin(60))});
\draw[thick,samples=50,smooth,domain=0:360] plot ({\radius*cos(\x)},{\radius*sin(\x)});
\draw[thick,samples=50,smooth,domain=0:360] plot ({\radius*(cos(\x)+cos(0))},{\radius*(sin(\x)+sin(0))});
\end{tikzpicture}