如何在 TikZ 中为这些圆圈着色?

如何在 TikZ 中为这些圆圈着色?

我怎样才能为这个 MWE 的输出着色,就像屏幕截图一样?

\documentclass{book}
\usepackage{tikz} 
\begin{document}
\begin{center}
\begin{tikzpicture}
\draw [thick] (3.25,-2) circle (1.5cm)      node[right=.7cm, above=.4cm] {$A$};
\draw [thick] (2.5,-3.5) circle (1.5cm)     node[left=.7cm,] {$B$};
\draw [thick] (4,-3.5) circle (1.5cm)       node[right=.7cm] {$C$};
\draw [thick](0,0) rectangle (6.5,-5.5)     node[left=6cm, above=.3cm] {$M$};
\end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

答案1

最简单的方法是先填充圆圈,然后再绘制它们。

\documentclass{book}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}
\fill[gray!50] (3.25,-2) circle (1.5cm);
\fill[gray!50] (2.5,-3.5) circle (1.5cm);
\fill[white] (4,-3.5) circle (1.5cm);
\draw [thick] (3.25,-2) circle (1.5cm)      node[right=.7cm, above=.4cm] {$A$};
\draw [thick] (2.5,-3.5) circle (1.5cm)     node[left=.7cm,] {$B$};
\draw [thick] (4,-3.5) circle (1.5cm)       node[right=.7cm] {$C$};
\draw [thick](0,0) rectangle (6.5,-5.5)     node[left=6cm, above=.3cm] {$M$};
\end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

您可以通过适当剪辑来为每个区域着色:

\documentclass{book}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}
\fill[gray!50] (3.25,-2) circle (1.5cm);
\fill[gray!50] (2.5,-3.5) circle (1.5cm);
\fill[yellow] (4,-3.5) circle (1.5cm);
\begin{scope}
  \clip (2.5,-3.5) circle (1.5cm);
  \fill[blue!50] (3.25,-2) circle (1.5cm);
\end{scope}
\begin{scope}
  \clip (3.25,-2) circle (1.5cm);
  \fill[red!50] (4,-3.5) circle (1.5cm);
\end{scope}
\begin{scope}
  \clip (2.5,-3.5) circle (1.5cm);
  \fill[olive!50] (4,-3.5) circle (1.5cm);
\end{scope}
\begin{scope}
  \clip (2.5,-3.5) circle (1.5cm);
  \clip (3.25,-2) circle (1.5cm);
  \fill[green!50] (4,-3.5) circle (1.5cm);
\end{scope}
\draw [thick] (3.25,-2) circle (1.5cm)      node[right=.7cm, above=.4cm] {$A$};
\draw [thick] (2.5,-3.5) circle (1.5cm)     node[left=.7cm,] {$B$};
\draw [thick] (4,-3.5) circle (1.5cm)       node[right=.7cm] {$C$};
\draw [thick](0,0) rectangle (6.5,-5.5)     node[left=6cm, above=.3cm] {$M$};
\end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

相关内容