[Tex/LaTex] stripe pattern tikz manual chapter 78

colordiagramsgraphicstikz-pgftransparency

Interleaving two colors in a stripes pattern

Aim: coloring a shape (rectangle, circle, etc.) in two interleaving colors with the aid of patterns.

One solution would be the definition of four patterns. As you can tell from the the following there are two interleaving patterns defined in each direction.

southwest — nordeast
swne
1- swnestripes: (blue stripes)
2- swneStripes: (white stripes)

southeast — nordwest
senw
3- senwstripes: (red stripes)
4- senwStripes: (white stripes)

Naturally, the other solution would be first filling the background in one color (e.g., white) and then applying a pattern on top of it (e.g.,blue). But I do like making things complicated 😉

Preamble:

\documentclass[a4paper]{article}
    \usepackage{graphicx}
    \usepackage{subfig}
    \usepackage{tikz}
    \usetikzlibrary{patterns}

Declarations:

    \begin{document}
    \def\boundb{(-3,2) rectangle (4,-2)}
    \def\setAa{(-1,0) circle (1)}
    \def\setBa{(2,0) circle (1)}
    \def\setAb{(0,0) circle (1)}
    \def\setBb{(1,0) circle (1)}

Patterns:

    \pgfdeclarepatternformonly{swnestripes}{\pgfpoint{0cm}{0cm}}{\pgfpoint{1cm}{1cm}}{\pgfpoint{1cm}{1cm}}
    {
        \foreach \i in {0.1cm, 0.3cm,...,0.9cm}
        {
         \pgfpathmoveto{\pgfpoint{\i}{0cm}}
         \pgfpathlineto{\pgfpoint{1cm}{1cm - \i}}
         \pgfpathlineto{\pgfpoint{1cm}{1cm - \i + 0.1cm}}
         \pgfpathlineto{\pgfpoint{\i - 0.1cm}{0cm}}
         \pgfpathclose%
         \pgfusepath{fill}
         \pgfpathmoveto{\pgfpoint{0cm}{\i}}
         \pgfpathlineto{\pgfpoint{1cm - \i}{1cm}}
         \pgfpathlineto{\pgfpoint{1cm - \i - 0.1cm}{1cm}}
         \pgfpathlineto{\pgfpoint{0cm}{\i + 0.1cm}}
         \pgfpathclose%
         \pgfusepath{fill}
        }
    }
    \pgfdeclarepatternformonly{swneStripes}{\pgfpoint{0cm}{0cm}}{\pgfpoint{1cm}{1cm}}{\pgfpoint{1cm}{1cm}}
    {
        \foreach \i in {0.1cm, 0.3cm,...,0.9cm}
        {
         \pgfpathmoveto{\pgfpoint{\i}{0cm}}
         \pgfpathlineto{\pgfpoint{1cm}{1cm - \i}}
         \pgfpathlineto{\pgfpoint{1cm}{1cm - \i - 0.1cm}}
         \pgfpathlineto{\pgfpoint{\i + 0.1cm}{0cm}}
         \pgfpathclose%
         \pgfusepath{fill}
         \pgfpathmoveto{\pgfpoint{0cm}{\i}}
         \pgfpathlineto{\pgfpoint{1cm - \i}{1cm}}
         \pgfpathlineto{\pgfpoint{1cm - \i + 0.1cm}{1cm}}
         \pgfpathlineto{\pgfpoint{0cm}{\i - 0.1cm}}
         \pgfpathclose%
         \pgfusepath{fill}
        }
    }
    \pgfdeclarepatternformonly{senwstripes}{\pgfpoint{0cm}{0cm}}{\pgfpoint{1cm}{1cm}}{\pgfpoint{1cm}{1cm}}
    {
        \foreach \i in {0.1cm, 0.3cm,...,0.9cm}
        {
         \pgfpathmoveto{\pgfpoint{0cm}{\i}}
         \pgfpathlineto{\pgfpoint{0cm}{\i + 0.1cm}}
         \pgfpathlineto{\pgfpoint{\i + 0.1cm}{0cm}}
         \pgfpathlineto{\pgfpoint{\i}{0cm}}
         \pgfpathclose%
         \pgfusepath{fill}
         \pgfpathmoveto{\pgfpoint{1cm}{\i}}
         \pgfpathlineto{\pgfpoint{\i}{1cm}}
         \pgfpathlineto{\pgfpoint{\i + 0.1cm}{1cm}}
         \pgfpathlineto{\pgfpoint{1cm}{\i + 0.1cm}}
         \pgfpathclose%
         \pgfusepath{fill}
        }
    }
    \pgfdeclarepatternformonly{senwStripes}{\pgfpoint{0cm}{0cm}}{\pgfpoint{1cm}{1cm}}{\pgfpoint{1cm}{1cm}}
    {
        \foreach \i in {0.0cm, 0.2cm,...,0.8cm}
        {
         \pgfpathmoveto{\pgfpoint{\i}{0cm}}
         \pgfpathlineto{\pgfpoint{0cm}{\i}}
         \pgfpathlineto{\pgfpoint{0cm}{\i + 0.1cm}}
         \pgfpathlineto{\pgfpoint{\i + 0.1cm}{0cm}}
         \pgfpathclose%
         \pgfusepath{fill}
         \pgfpathmoveto{\pgfpoint{1cm}{\i}}
         \pgfpathlineto{\pgfpoint{\i}{1cm}}
         \pgfpathlineto{\pgfpoint{\i + 0.1cm}{1cm}}
         \pgfpathlineto{\pgfpoint{1cm}{\i + 0.1cm}}
         \pgfpathclose%
         \pgfusepath{fill}
        }
    }

Figure:

    \begin{figure}[p]
    \centering
    \subfloat[Two separate Sets $\overline{A\cup B}$]{
     \begin{tikzpicture}[scale=0.6, auto, swap]
       \filldraw[pattern=senwStripes, pattern color=magenta!60]\boundb;
       \filldraw[pattern=swnestripes, pattern color=blue!60]\boundb;
       \filldraw[fill=white, draw=blue, line width=2pt]\setAa;
       \filldraw[fill=white, draw=magenta, line width=2pt]\setBa;
       \node at (-2,1) [above left] {$A$};
       \node at (1,1) [above left] {$B$};
     \end{tikzpicture}
    }\qquad
    \caption{Set Examples}
    \label{fig:Sets}
    \end{figure}
\end{document}

Issues:

pdflatex produces

ptptpt    pt    ptptpt    pt    ptptpt    pt    ptptpt    pt

(The desired figure is also created, on a separate page, as expected.)

Dominant Color

When drawing two or more patterns on one shape (e.g., pattern shifted, different color, see interleaved stripes above, multiple possible combinations) in the end, one shape or color is overlapping the other, i.e., one color is more dominant in the whole figure.

   \filldraw[pattern=senwStripes, pattern color=magenta!60]\boundb;
   \filldraw[pattern=swnestripes, pattern color=blue!60]\boundb;

blue is the dominant in the figure.

   \filldraw[pattern=swnestripes, pattern color=blue!60]\boundb;
   \filldraw[pattern=senwStripes, pattern color=magenta!60]\boundb;

magenta is. You can have a look at the effect if you compile the following code:

    \begin{figure}[p]
    \centering
    \subfloat[blue dominant $\overline{A\cup B}$]{
     \begin{tikzpicture}[scale=0.6, auto, swap]
       \filldraw[pattern=senwStripes, pattern color=magenta!60]\boundb;
       \filldraw[pattern=swnestripes, pattern color=blue!60]\boundb;
       \filldraw[fill=white, draw=blue, line width=2pt]\setAa;
       \filldraw[fill=white, draw=magenta, line width=2pt]\setBa;
       \node at (-2,1) [above left] {$A$};
       \node at (1,1) [above left] {$B$};
     \end{tikzpicture}
    }\qquad
    \subfloat[magenta dominant $\overline{A\cup B}$]{
     \begin{tikzpicture}[scale=0.6, auto, swap]
       \filldraw[pattern=swnestripes, pattern color=blue!60]\boundb;
       \filldraw[pattern=senwStripes, pattern color=magenta!60]\boundb; % swapped
       \filldraw[fill=white, draw=blue, line width=2pt]\setAa;
       \filldraw[fill=white, draw=magenta, line width=2pt]\setBa;
       \node at (-2,1) [above left] {$A$};
       \node at (1,1) [above left] {$B$};
     \end{tikzpicture}
    }\qquad
    \caption{Set Examples}
    \label{fig:Sets}
    \end{figure}
\end{document}

Hypothetical question: is there a way to make them both "equal" dominant?

Here is another example:

    \begin{figure}[p]
    \centering
    \subfloat[Set B overlapping set A $\overline{A\cup B}$]{
    \begin{tikzpicture}[scale=0.6, auto, swap]

      \node at (-3,1.5) [above left] {$\scriptstyle U$};
      \begin{scope}[fill opacity=0.2]
       \filldraw[pattern=swnestripes, pattern color=blue!80, ultra thin]\boundb;
       \filldraw[pattern=senwStripes, pattern color=orange, ultra thin]\boundb;
      \end{scope}

      \begin{scope}
       \clip\boundb;
       \fill[fill=white]\setAb;
      \end{scope}
      \begin{scope}
       \clip\boundb;
       \fill[fill=white]\setBb;
      \end{scope}

      \begin{scope}
       \clip\setAb;
       \fill[fill=red!10]\setBb;
      \end{scope}
      \begin{scope}
       \clip\setBb;
       \fill[fill=red!10]\setAb;
      \end{scope}

      \draw[draw=blue, line width=1pt]\setAb;
      \draw[draw=orange, line width=1pt]\setBb;

      \node at (-1,1) [above left] {$\scriptstyle A$};
      \node at (2,1) [above right] {$\scriptstyle B$};
    \end{tikzpicture}
    }\qquad
    \caption{Set Examples}
    \label{fig:Sets}
    \end{figure}
\end{document}

In the following figure, orange circle is overlapping blue circle at intersection points.

enter image description here

Since transparency does not "equally" distribute the dominance of colors in the overlapping parts (the lower color contributes less on the overall effect), one solution for "equal rights" for the colors is structured drawing.

enter image description here
enter image description here

Can you think of a "hack" to get there?

How can I get rid of the "ptptpt"? Any help appreciated. Thanks.


If this looks a bit strange to you, here is a compromise I decided on:

compromise

One color may overlap in the upper part, and one in the lower!

    \begin{figure}[p]
    \centering
    \subfloat[Set B overlapping set A and set A overlapping set B $\overline{A\cup B}$]{
    \begin{tikzpicture}[scale=0.6, auto, swap]

      \node at (-3,1.5) [above left] {$\scriptstyle U$};
      \begin{scope}[fill opacity=0.2]
       \filldraw[pattern=swnestripes, pattern color=blue!80, ultra thin]\boundb;
       \filldraw[pattern=senwStripes, pattern color=orange, ultra thin]\boundb;
      \end{scope}

      \begin{scope}
       \clip\boundb;
       \fill[fill=white]\setAb;
      \end{scope}
      \begin{scope}
       \clip\boundb;
       \fill[fill=white]\setBb;
      \end{scope}

      \begin{scope}
       \clip\setAb;
       \fill[fill=red!10]\setBb;
      \end{scope}
      \begin{scope}
       \clip\setBb;
       \fill[fill=red!10]\setAb;
      \end{scope}

      \node at (-1,1) [above left] {$\scriptstyle A$};
      \node at (2,1) [above right] {$\scriptstyle B$};

      \begin{scope}
        \clip (-2,0) rectangle (4,2);
        \draw[draw=blue, line width=1pt]\setAb;
        \draw[draw=orange, line width=1pt]\setBb;
      \end{scope}
      \begin{scope}
        \clip (-2,0) rectangle (4,-2);
        \draw[draw=orange, line width=1pt]\setBb;
        \draw[draw=blue, line width=1pt]\setAb;
      \end{scope}
    \end{tikzpicture}
    }\qquad
    \caption{Set Examples}
    \label{fig:Sets}
    \end{figure}

Best Answer

To get the circles as you draw them, use a clip. Draw one of the circles twice, once clipped against the rectangle \clip (-2,2) rectangle (.5,-2); and once against \clip (2,2) rectangle (.5,-2);. So in your code above, put:

  \begin{scope}
  \clip (2,2) rectangle (.5,-2);
  \draw[draw=blue, line width=1pt]\setAb;
  \end{scope}
  \draw[draw=orange, line width=1pt]\setBb;
  \begin{scope}
  \clip (-2,2) rectangle (.5,-2);
  \draw[draw=blue, line width=1pt]\setAb;
  \end{scope}

With vastly exaggerated line width you get:

overlapping circles