1

I currently have a diagram involving four circles and a square connecting all four centers. However, I'm trying to figure out how to shade the inner area of the four circles. My current code looks like this, and is simply the diagram without the central shaded area.

\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);
        \clip (X) circle(\radius);
        \clip (Y) circle(\radius);
        \fill[gray, opacity=0.5] (Z) circle(\radius);
    \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}
\end{document}

enter image description here

6
  • 2
    Welcome to TeX.SE! Would you mind completing your code with a \begin{document} and everything? It really helps answerers. Your problem can be solved by enclosing the area with a path and then using fill. Your code is very clear and well documented by the way.
    – Jasper
    Commented Sep 3 at 23:11
  • \fill (X) -- ++(-\radius,0pt) arc (180:270:\radius) -- cycle; for the X one, for example.
    – cfr
    Commented Sep 4 at 3:33
  • Hi! Thanks for letting me know and complimenting my code! I've updated the code including the packages.
    – andorooo
    Commented Sep 4 at 22:33
  • thanks for editing! which bit are you trying to shade? the bit of each circle inside the square? or the bit of the square not in any circle? (but note your code could not be compiled as-was.)
    – cfr
    Commented Sep 4 at 23:53
  • your code says you are trying to shade the overlap of the four circles, but there is no overlap. so ...?
    – cfr
    Commented Sep 5 at 0:01

2 Answers 2

5

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 circle1.

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

filling the circles which overlap with the square; filling the square not overlapped by the circles

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.

2
  • Thank you so much! Really really appreciate this
    – andorooo
    Commented Sep 5 at 3:50
  • Thank you also for taking the time to clarify parts of my code that were incorrect for this diagram, and teaching me through your thought process in what you've designed here.
    – andorooo
    Commented Sep 5 at 3:51
2

Here is two ways to achieved this.

If you draw the square first, then the circles will hide the unwanted part.

\documentclass{standalone}
\usepackage{tikz}
\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

    % fill the square
    \fill[gray,opacity=0.5] (W) -- (X) -- (Z) -- (Y) -- cycle;

    % Draw the circles and hide the unwanted part
    \filldraw[fill=white] (W) circle(\radius);
    \filldraw[fill=white] (X) circle(\radius);
    \filldraw[fill=white] (Y) circle(\radius);
    \filldraw[fill=white] (Z) circle(\radius);
    
    % 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}
\end{document}

Second, with the arc command, you can run along the circles to create the path you need. I started it at the mid point between W and X, then run along in a counter clockwise motion.

\documentclass{standalone}
\usepackage{tikz}
\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 and hide the unwanted part
    \draw (W) circle(\radius);
    \draw (X) circle(\radius);
    \draw (Y) circle(\radius);
    \draw (Z) circle(\radius);
    
    % fill the center part
    \fill[gray,opacity=0.5] ($0.5*(W)+0.5*(X)$) arc(0:-90:\radius) arc(90:0:\radius) arc(180:90:\radius) arc(270:180:\radius) -- cycle;

    % 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}
\end{document}
2
  • Oh my goodness thank you so much for this! I've been scratching my head trying to figure out the most efficient method.
    – andorooo
    Commented Sep 5 at 0:26
  • Thank you Alain
    – andorooo
    Commented Sep 5 at 21:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .