# 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
1- swnestripes: (blue stripes) 2- swneStripes: (white stripes)

southeast -- nordwest
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}
\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}
\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}
\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}
\caption{Set Examples}
\label{fig:Sets}
\end{figure}
\end{document}


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

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.

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:

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}
\caption{Set Examples}
\label{fig:Sets}
\end{figure}

-
Please add a preamble with all packages and libraries. Guessing them is not fun. –  Caramdir Jun 15 '11 at 20:32

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:

-
Great! Thank you. Ok, but I have to admit - this looks a bit strange ;) –  panny Jun 20 '11 at 21:30

The pts seem to be a bug in \foreach. If you replace {0.1cm,0.3cm,...,0.9cm} by {0.1,0.3,...,0.9} and add cm an the appropriate places in the body, it works:

\pgfdeclarepatternformonly{stripes}{\pgfpoint{0cm}{0cm}}{\pgfpoint{1cm}{1cm}}{\pgfpoint{1cm}{1cm}}
{
\foreach \i in {0.1, 0.3,...,0.9}
{
\pgfpathmoveto{\pgfpoint{\i cm}{0cm}}
\pgfpathlineto{\pgfpoint{1cm}{1cm - \i cm}}
\pgfpathlineto{\pgfpoint{1cm}{1cm - \i cm+ 0.1cm}}
\pgfpathlineto{\pgfpoint{\i cm - 0.1cm}{0cm}}
\pgfpathclose%
\pgfusepath{fill}
\pgfpathmoveto{\pgfpoint{0cm}{\i cm}}
\pgfpathlineto{\pgfpoint{1cm - \i cm}{1cm}}
\pgfpathlineto{\pgfpoint{1cm - \i cm - 0.1cm}{1cm}}
\pgfpathlineto{\pgfpoint{0cm}{\i cm + 0.1cm}}
\pgfpathclose%
\pgfusepath{fill}
}
}

-
thank you, pts are gone –  panny Jun 15 '11 at 22:58
I wouldn't call that a bug. Without having tested it, I'd say that foreach is not sophisticated enough to realise that the partial sequence 0.1cm, 0.3cm,...,0.9cm should be filled in with 0.5cm,0.7cm. –  Loop Space Jun 16 '11 at 6:47
@Andrew: Put printing pt instead of just failing is a bit strange behavior. –  Caramdir Jun 16 '11 at 14:59
Not actually all that silly (looking at the code). To do arithmetic, it sticks all the values in to lengths so there's lots of \ifdim\pgffor@dotsend pt>\pgffor@last pt. If \pgffor@dotsend and \pgffor@last already have dimensions, this adds the spurious pts. "Doctor, doctor, my head hurts when I hit it with a hammer." "So don't do it.". –  Loop Space Jun 20 '11 at 19:11