# TikZ shaded translated regions defined by boundaries of shape nodes and lines

Given a lattice made up of shapes, how do I shade the regions (and their translates) defined by the boundaries of these nodes and lines going through these shapes?

In my particular case, I have a region containing the origin, partitioned by two lines (spanned by vectors $b_1$ and $b_2$) going through origin (denoted by $A,B,C,D$) and their translates ($A',C',D'$) inside the given parallelogram. These labelled regions are those that we want to be shaded.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,calc,shapes,decorations}

\begin{document}

\begin{tikzpicture}[scale=2]
\coordinate (Origin)   at (0,0);
\coordinate (XAxisMin) at (-1.5,0);
\coordinate (XAxisMax) at (4,0);
\coordinate (YAxisMin) at (0,-0.5);
\coordinate (YAxisMax) at (0,3.3);
\draw [thin] (XAxisMin) -- (XAxisMax);% Draw x axis
\draw [thin] (YAxisMin) -- (YAxisMax);% Draw y axis

\draw[blue] (-0.25,0.25) node {$\mathbf{A}$};
\draw[blue] (0.35,0.25) node {$\mathbf{B}$};
\draw[blue] (-0.3,-0.25) node {$\mathbf{C}$};
\draw[blue] (0.25,-0.25) node {$\mathbf{D}$};

\clip (-0.7,-0.7) rectangle (4.18cm,3.3cm); % Clips the picture...
\pgftransformcm{1}{0.3}{0.7}{1}{\pgfpoint{0cm}{0cm}}

\coordinate (Bone) at (0,2);
\coordinate (Btwo) at (2,-2);
\coordinate (negBonePlusBtwo) at (-2,0);
\draw[style=help lines,dashed] (-14,-14) grid[step=2cm] (14,14);

\foreach \x in {-1,0,...,4}{
\foreach \y in {-1,0,...,4}{
\node[star,scale=5,draw=black] at (2 * \x, 2 * \y) {};
% make a lattice of stars
}
}

\draw[very thick,draw=black] (Origin)
rectangle ($2*(Bone)+(Btwo)$);

\draw [ultra thick,-latex,red] (Origin)
-- (Bone) node [above left] {$b_1$};

\draw[blue] (Bone) node [below right] {$\mathbf{D} '$};
\draw[blue] ($(Bone)+(Btwo)$) node [above left] {$\mathbf{A} '$};
\draw[blue] ($1.93*(Bone)+(Btwo)$) node [below left] {$\mathbf{C} '$};

\draw [ultra thick,-latex,red] (Origin)
-- ($(Bone)+(Btwo)$) node [below right] {$b_2$};

\draw [thick,dashed,-latex,red,scale=3] (Origin) -- ($-0.25*(Bone)$) ;
\draw [thick,dashed,-latex,red] (Origin) -- ($0.35*(negBonePlusBtwo)$) ;

\end{tikzpicture}
\end{document}

-
If you add the following lines at the end of your code \clip (Origin) rectangle (Bone -| Btwo);\node[star,scale=5,draw=black,fill=yellow] at (0,2) {}; you can get an idea how to proceed I hope. So you can first fill the stars and then place the vectors. Also in question tex.stackexchange.com/questions/42564 we didn't have anything else to draw but here you might constrain the effect of the transformation matrix for other additions –  percusse May 29 at 9:06

This is a possible solution just exploiting different pgflayers to deploy things:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,calc,shapes,decorations,backgrounds}
\pgfdeclarelayer{foreground}
\pgfsetlayers{main,foreground}

\begin{document}

\begin{tikzpicture}[scale=2]
\coordinate (Origin)   at (0,0);
\coordinate (XAxisMin) at (-1.5,0);
\coordinate (XAxisMax) at (4,0);
\coordinate (YAxisMin) at (0,-0.5);
\coordinate (YAxisMax) at (0,3.3);

\begin{pgfonlayer}{foreground} % let's start drawing on the top level all the annotations
\draw [thin] (XAxisMin) -- (XAxisMax);% Draw x axis
\draw [thin] (YAxisMin) -- (YAxisMax);% Draw y axis

\draw[blue] (-0.25,0.25) node {$\mathbf{A}$};
\draw[blue] (0.35,0.25) node {$\mathbf{B}$};
\draw[blue] (-0.3,-0.25) node {$\mathbf{C}$};
\draw[blue] (0.25,-0.25) node {$\mathbf{D}$};

\clip (-0.7,-0.7) rectangle (4.18cm,3.3cm); % Clips the picture...
\pgftransformcm{1}{0.3}{0.7}{1}{\pgfpoint{0cm}{0cm}}

\coordinate (Bone) at (0,2);
\coordinate (Btwo) at (2,-2);
\coordinate (negBonePlusBtwo) at (-2,0);

\draw[style=help lines,dashed] (-14,-14) grid[step=2cm] (14,14);

\draw[very thick,draw=black] (Origin)
rectangle ($2*(Bone)+(Btwo)$);

\draw [ultra thick,-latex,red] (Origin)
-- (Bone) node [above left] {$b_1$};

\draw[blue] (Bone) node [below right] {$\mathbf{D} '$};
\coordinate (Aone) at ($(Bone)+(Btwo)$);
\draw[blue] (Aone) node [above left] {$\mathbf{A} '$};
\coordinate (Cone) at ($2*(Bone)+(Btwo)$);
\draw[blue] (Cone) node [below left=0.2cm] {$\mathbf{C} '$};

\draw [ultra thick,-latex,red] (Origin)
-- ($(Bone)+(Btwo)$) node [below right] {$b_2$};

\draw [thick,dashed,-latex,red,scale=3] (Origin) -- ($-0.25*(Bone)$) ;
\draw [thick,dashed,-latex,red] (Origin) -- ($0.35*(negBonePlusBtwo)$) ;
\end{pgfonlayer}

% the filling is on the main layer in the middle
\fill[gray!25](Origin)--(Bone)--(Cone)--(Aone);

% the solution needs an extra clipping for the grid
\clip (-0.7,-0.7) rectangle (4.18cm,3.3cm); % Clip again
\pgftransformcm{1}{0.3}{0.7}{1}{\pgfpoint{0cm}{0cm}}
\foreach \x in {-1,0,...,4}{
\foreach \y in {-1,0,...,4}{
\node[star,scale=5,draw=black,fill=white] at (2 * \x, 2 * \y) {}; % with fill=white the stars are drawn on the top of the filling
% make a lattice of stars
}
}
\end{tikzpicture}

\end{document}


The result:

-
Hmm I got the question wrong then. I thought the complement of the shaded region inside the parallelogram should be filled. –  percusse May 29 at 12:45
Well perhaps I got it wrong (most likeley)... We should wait OP's news about that :) –  Claudio Fiandrino May 29 at 12:49
You got it quite different from what I want to do. But I appreciate some things I learned from there. In my example, I want to color A and A' green, B blue, C and C' purple, D and D' yellow. –  user31437 May 30 at 4:39

It is probably easiest to put these shadings on a background layer. The following shades the complements of the nodes. Below that I'll show how shade the segments of the nodes:

\documentclass{article}
\usepackage{tikz,etoolbox}
\usetikzlibrary{matrix,calc,shapes,decorations}

\pgfdeclarelayer{background}
\pgfdeclarelayer{main}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\begin{document}

\begin{tikzpicture}[scale=2]
\coordinate (Origin)   at (0,0);
\coordinate (XAxisMin) at (-1.5,0);
\coordinate (XAxisMax) at (4,0);
\coordinate (YAxisMin) at (0,-0.5);
\coordinate (YAxisMax) at (0,3.3);

\begin{pgfonlayer}{foreground}
\draw [thin] (XAxisMin) -- (XAxisMax);% Draw x axis
\draw [thin] (YAxisMin) -- (YAxisMax);% Draw y axis
\end{pgfonlayer}

\clip (-0.7,-0.7) rectangle (4.18cm,3.3cm); % Clips the picture...
\begin{pgfonlayer}{background}
\clip (-0.7,-0.7) rectangle (4.18cm,3.3cm); % Clips the picture...
\end{pgfonlayer}
\begin{pgfonlayer}{foreground}
\clip (-0.7,-0.7) rectangle (4.18cm,3.3cm); % Clips the picture...

\draw[blue] (-0.25,0.25) node {$\mathbf{A}$};
\draw[blue] (0.35,0.25) node {$\mathbf{B}$};
\draw[blue] (-0.3,-0.25) node {$\mathbf{C}$};
\draw[blue] (0.25,-0.25) node {$\mathbf{D}$};
\end{pgfonlayer}

\pgftransformcm{1}{0.3}{0.7}{1}{\pgfpoint{0cm}{0cm}}

\coordinate (Bone) at (0,2);
\coordinate (Btwo) at (2,-2);
\coordinate (negBonePlusBtwo) at (-2,0);

\begin{pgfonlayer}{foreground}
\draw[style=help lines,dashed] (-14,-14) grid[step=2cm] (14,14);
\end{pgfonlayer}

\begin{pgfonlayer}{background}
\foreach \x in {-2,-1,...,3}{
\foreach \y in {-2,-1,...,4}{
\pgfmathparse{int(3*(\x+\y+4))}
\fill[red!\pgfmathresult] (2*\x,2*\y) -- +(2,0) -- +(2,2) -- +(0,2) --cycle;
}
}

\fill[red!40] (0,0) -- +(2,0) -- +(2,2) -- +(0,2) --cycle;
\end{pgfonlayer}

\foreach \x in {-1,0,...,4}{
\foreach \y in {-1,0,...,4}{
\node[star,scale=5,draw=black,fill=white] at (2 * \x, 2 * \y) {};
% make a lattice of stars
}
}

\begin{pgfonlayer}{foreground}
\draw[very thick,draw=black] (Origin) rectangle ($2*(Bone)+(Btwo)$);

\draw [ultra thick,-latex,red] (Origin) -- (Bone) node [above left]
{$b_1$};

\draw[blue] (Bone) node [below right] {$\mathbf{D} '$}; \draw[blue]
($(Bone)+(Btwo)$) node [above left] {$\mathbf{A} '$}; \draw[blue]
($1.95*(Bone)+0.96*(Btwo)$) node [below left] {$\mathbf{C} '$};

\draw [ultra thick,-latex,red] (Origin) -- ($(Bone)+(Btwo)$) node
[below right] {$b_2$};

\draw [thick,dashed,-latex,red,scale=3] (Origin) -- ($-0.25*(Bone)$)
; \draw [thick,dashed,-latex,red] (Origin) --
($0.35*(negBonePlusBtwo)$) ;
\end{pgfonlayer}
\end{tikzpicture}
\end{document}


The layers need to be declared outside of the picture and need to include a layer main. Also you have to provide the clip specification for each layer. In your case it is easiest before you transform the coordinates, hence the multiple clip commands early in the code. Placing the nodes on the main layer, allows one to fill them white covering the background, but still lying behind the foreground.

Now for shading the segments of the nodes instead, we can just use the anchors provided by the shape star to fill individual regions:

\documentclass{article}
\usepackage{tikz,etoolbox}
\usetikzlibrary{matrix,calc,shapes,decorations}

\pgfdeclarelayer{background}
\pgfdeclarelayer{main}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\begin{document}

\begin{tikzpicture}[scale=2]
\coordinate (Origin)   at (0,0);
\coordinate (XAxisMin) at (-1.5,0);
\coordinate (XAxisMax) at (4,0);
\coordinate (YAxisMin) at (0,-0.5);
\coordinate (YAxisMax) at (0,3.3);

\begin{pgfonlayer}{foreground}
\draw [thin] (XAxisMin) -- (XAxisMax);% Draw x axis
\draw [thin] (YAxisMin) -- (YAxisMax);% Draw y axis
\end{pgfonlayer}

\clip (-0.7,-0.7) rectangle (4.18cm,3.3cm); % Clips the picture...
\begin{pgfonlayer}{background}
\clip (-0.7,-0.7) rectangle (4.18cm,3.3cm);
\end{pgfonlayer}
\begin{pgfonlayer}{foreground}
\clip (-0.7,-0.7) rectangle (4.18cm,3.3cm);

\draw[blue] (-0.2,0.2) node {$\mathbf{A}$};
\draw[blue] (0.28,0.2) node {$\mathbf{B}$};
\draw[blue] (-0.28,-0.21) node {$\mathbf{C}$};
\draw[blue] (0.2,-0.2) node {$\mathbf{D}$};
\end{pgfonlayer}

\pgftransformcm{1}{0.3}{0.7}{1}{\pgfpoint{0cm}{0cm}}

\coordinate (Bone) at (0,2);
\coordinate (Btwo) at (2,-2);
\coordinate (negBonePlusBtwo) at (-2,0);

\begin{pgfonlayer}{foreground}
\draw[style=help lines,dashed] (-14,-14) grid[step=2cm] (14,14);
\end{pgfonlayer}

\foreach \x in {-1,0,...,4}{
\foreach \y in {-1,0,...,4}{
\node[star,scale=5,draw=black] (x) at (2 * \x, 2 * \y) {};
\begin{pgfonlayer}{background}
\fill[green] (x.outer point 3) -- (x.center) -- (x.outer
point 5) -- (x.inner point 4) -- (x.outer point 4) --
(x.inner point 3) -- cycle;
\fill[yellow] (x.inner point 2) -- (x.center) -- (x.outer
point 3);
\fill[green!30] (x.inner point 5) -- (x.center) -- (x.inner
point 2) -- (x.outer point 2) -- (x.inner point 1) --
(x.outer point 1) -- cycle;
\fill[yellow!30] (x.outer point 5) -- (x.center) -- (x.inner
point 5) -- cycle;
\end{pgfonlayer}
}
}

\begin{pgfonlayer}{foreground}
\draw[very thick,draw=black] (Origin) rectangle ($2*(Bone)+(Btwo)$);

\draw [ultra thick,-latex,red] (Origin) -- (Bone) node [above left]
{$b_1$};

\draw[blue] (Bone) node [below right] {$\mathbf{D} '$}; \draw[blue]
($(Bone)+(Btwo)$) node [above left] {$\mathbf{A} '$}; \draw[blue]
($1.95*(Bone)+0.96*(Btwo)$) node [below left] {$\mathbf{C} '$};

\draw [ultra thick,-latex,red] (Origin) -- ($(Bone)+(Btwo)$) node
[below right] {$b_2$};

\draw [thick,dashed,-latex,red,scale=3] (Origin) -- ($-0.25*(Bone)$)
; \draw [thick,dashed,-latex,red] (Origin) --
($0.35*(negBonePlusBtwo)$) ;
\end{pgfonlayer}
\end{tikzpicture}
\end{document}


The anchors are labelled (x.inner point n) and (x.outer point n) with n running from 1 to 5.

In general, you lattice dirrections may not coincide nicely with anchors in your shape. In such cases, you can recreate the boundary path of the shape and use that to clip. In your case the node loop would be:

\foreach \x in {-1,0,...,4}{
\foreach \y in {-1,0,...,4}{
\coordinate (x) at (2 * \x, 2 * \y);
\node[star,scale=5,draw=black] (n) at (x) {};
\begin{pgfonlayer}{background}
\begin{scope}
\path[clip] (n.outer point 1) -- (n.inner point 1) --
(n.outer point 2) -- (n.inner point 2) --
(n.outer point 3) -- (n.inner point 3) --
(n.outer point 4) -- (n.inner point 4) --
(n.outer point 5) -- (n.inner point 5) --  cycle;
\fill[green] (x) -- +(1,0) -- +(1,-1) -- +(0,-1) -- cycle;
\fill[yellow] (x) -- +(0,-1) -- +(-1,-1) -- +(-1,0) -- cycle;
\fill[green!30] (x) -- +(-1,0) -- +(-1,1) -- +(0,1) -- cycle;
\fill[yellow!30] (x) -- +(0,1) -- +(1,1) -- +(1,0) -- cycle;
\end{scope}
\end{pgfonlayer}
}
}


which produces the same result as the previous picture.

The techniques at Clipping a path using a node might enable you to just use the node to do the clipping...

-
Interesting. How should we shade the defined subregions inside the star nodes? –  user31437 May 30 at 4:41