The first is a very slight modification of the code you posted, which shades the parts of the circles within the square.
The second is a more significant adaption which shades the part of the square not in any circle. This example uses a couple of tricks to make the code more concise e.g. it uses a loop to set the coordinates and create the nodes. It uses a combination of clipping and the even odd rule
for filling.
\begin{tikzpicture}
% Circle radius
\def\radius{1.5cm}
Given the radius, we can calculate the distance from the origin of each coordinate quite easily.
\newdimen\cdist
\pgfmathsetlength\cdist{sqrt(2*((\radius)^2))}
This enables us to use polar coordinates to place W
, X
, Y
and Z
, which makes it easy to do it in a loop without having to worry about combinations of negative and positive horizontal/vertical displacement. We also use the updated syntax for circle
1.
\foreach \i/\j in {W/135,X/45,Y/-135,Z/-45} \draw (\j:\cdist) coordinate (\i) circle [radius=\radius] (\j:{0.2cm+\cdist}) node {$\i$};
The last part adds the node at the same time, using an adjusted polar coordinate. We then start our scope
\begin{scope}
clipping to the square
\clip (X) |- (Y) |- cycle;
and using the even odd rule
to fill the square minus the circles. (If we did not use the clipping above, the parts of the circles outside the square would also be filled, but the parts inside the square would not.)
\path [fill=gray,opacity=0.5,postaction=draw,even odd rule] (W) -- (X) -- (Z) -- (Y) -- cycle (W) circle (\radius) (X) circle (\radius) (Y) circle (\radius) (Z) circle (\radius) ;
\end{scope}
\end{tikzpicture}
Complete code:
\documentclass{standalone}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
% Circle radius
\def\radius{1.5cm}
% Positions of the centers of the circles
\coordinate (W) at (-\radius,\radius); % Top-left
\coordinate (X) at (\radius,\radius); % Top-right
\coordinate (Y) at (-\radius,-\radius); % Bottom-left
\coordinate (Z) at (\radius,-\radius); % Bottom-right
% Draw the circles
\draw (W) circle(\radius);
\draw (X) circle(\radius);
\draw (Y) circle(\radius);
\draw (Z) circle(\radius);
% Shade the central overlap of the four circles
\begin{scope}
\clip (W) circle(\radius)
(X) circle(\radius)
(Y) circle(\radius)
(Z) circle(\radius);
% \fill[gray, opacity=0.5] (Z) circle(\radius);
\draw [fill=gray,opacity=0.5,postaction=draw] (W) -- (X) -- (Z) -- (Y) -- cycle;
\end{scope}
% Draw lines connecting W to X to Z to Y
% \draw (W) -- (X) -- (Z) -- (Y) -- cycle;
% Label the centers in math mode, positioned closer to the box
\node at ($(W) + (-0.2,0.2)$) {\(W\)};
\node at ($(X) + (0.2,0.2)$) {\(X\)};
\node at ($(Y) + (-0.2,-0.2)$) {\(Y\)};
\node at ($(Z) + (0.2,-0.2)$) {\(Z\)};
\end{tikzpicture}
\begin{tikzpicture}
% Circle radius
\def\radius{1.5cm}
\newdimen\cdist
\pgfmathsetlength\cdist{sqrt(2*((\radius)^2))}
\foreach \i/\j in {W/135,X/45,Y/-135,Z/-45} \draw (\j:\cdist) coordinate (\i) circle [radius=\radius] (\j:{0.2cm+\cdist}) node {$\i$};
\begin{scope}
\clip (X) |- (Y) |- cycle;
\path [fill=gray,opacity=0.5,postaction=draw,even odd rule] (W) -- (X) -- (Z) -- (Y) -- cycle (W) circle (\radius) (X) circle (\radius) (Y) circle (\radius) (Z) circle (\radius) ;
\end{scope}
\end{tikzpicture}
\end{document}
1Because Somebody will tell me off otherwise.
\begin{document}
and everything? It really helps answerers. Your problem can be solved by enclosing the area with apath
and then usingfill
. Your code is very clear and well documented by the way.\fill (X) -- ++(-\radius,0pt) arc (180:270:\radius) -- cycle;
for the X one, for example.