9

I'm trying to produce a copy of the right hand side of this picture using TikZ shadings.

Trying to emulate both patterns on the right

I thought it would be a simple task but it has proven itself difficult (at least for me).

First I couldn't find a way to make that kind of shading inclined by default, so I opted to make it straight and then rotate the outcome and then clip it. The thing is, when rotating the figure ends up in a werid spot of the document (not centered). So I don't know exactly how to produce both patterns, rotate them, clip them and then placing them one under the other.

This is what I managed to do until now:

\begin{figure}[!h]
\begin{tikzpicture}%[transform canvas={rotate=45}]
  \pgflowlevelobj{\pgftransformrotate{45}}{
 \foreach \i in {-3,...,3}
    {\shade [shading=axis,shading angle=90, xscale = 0.3] (2*\i,-10) rectangle (2*\i+1,-14);
    \shade [shading=axis,shading angle=-90, xscale = 0.3] (2*\i+1,-10) rectangle (2*\i+2,-14);};}
\end{tikzpicture}
\end{figure}

Thanks in advance!

Ewajs
  • 255
  • 1
  • 8

2 Answers2

8

Taking advantage of what you've already done, but changing a little the coordinates; for the lower part I used a custom pattern which is a variation of north east lines, but allowing you to specify the separation between the lines, their width and color:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns}

% defining the new dimensions and parameters
\newlength{\hatchspread}
\newlength{\hatchthickness}
\newlength{\hatchshift}
\newcommand{\hatchcolor}{}
% declaring the keys in tikz
\tikzset{hatchspread/.code={\setlength{\hatchspread}{#1}},
         hatchthickness/.code={\setlength{\hatchthickness}{#1}},
         hatchshift/.code={\setlength{\hatchshift}{#1}},% must be >= 0
         hatchcolor/.code={\renewcommand{\hatchcolor}{#1}}}
% setting the default values
\tikzset{hatchspread=3pt,
         hatchthickness=0.4pt,
         hatchshift=0pt,% must be >= 0
         hatchcolor=black}
%\pgfdeclarepatternformonly{north east lines}{\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{4pt}{4pt}}{\pgfqpoint{3pt}{3pt}}%
%{
%  \pgfsetlinewidth{0.4pt}
%  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
%  \pgfpathlineto{\pgfqpoint{3.1pt}{3.1pt}}
%  \pgfusepath{stroke}
%}

% declaring the pattern
\pgfdeclarepatternformonly[\hatchspread,\hatchthickness,\hatchshift,\hatchcolor]% variables
   {custom north east lines}% name
   {\pgfqpoint{\dimexpr-2\hatchthickness}{\dimexpr-2\hatchthickness}}% lower left corner
   {\pgfqpoint{\dimexpr\hatchspread+2\hatchthickness}{\dimexpr\hatchspread+2\hatchthickness}}% upper right corner
   {\pgfqpoint{\dimexpr\hatchspread}{\dimexpr\hatchspread}}% tile size
   {% shape description
    \pgfsetlinewidth{\hatchthickness}
    \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
    \pgfpathlineto{\pgfqpoint{\dimexpr\hatchspread+0.15pt+\hatchshift}{\dimexpr\hatchspread+0.15pt+\hatchshift}}
    \ifdim \hatchshift > 0pt
      \pgfpathmoveto{\pgfqpoint{0pt}{\hatchshift}}
      \pgfpathlineto{\pgfqpoint{\dimexpr0.15pt+\hatchshift}{-0.15pt}}
    \fi
    \pgfsetstrokecolor{\hatchcolor}
%    \pgfsetdash{{1pt}{1pt}}{0pt}% dashing cannot work correctly in all situation this way
    \pgfusepath{stroke}
   }

\begin{document}

\centering
\begin{tikzpicture}
\begin{scope}
\clip
  (-4,-1.7) -- (-4,1) -- (4,1) -- (4,-2) -- cycle;
\begin{scope}[transform canvas={rotate around={12.5:(3,0)}}]
\foreach \i in {-8,...,8}
  {
   \shade [overlay,shading=axis,shading angle=90, xscale = 0.3] (2*\i,3) rectangle (2*\i+1,-3);
   \shade [overlay,shading=axis,shading angle=-90, xscale = 0.3] (2*\i+1,3) rectangle (2*\i+2,-3);
  }
\end{scope}
\end{scope}
\fill[gray!15]
    (-4,-1.7) -- (-4,-5) -- (4,-5) -- (4,-2) -- cycle;    
\fill[
  pattern=custom north east lines,
  hatchspread=4.5pt,
  hatchthickness=1.35pt,
  hatchcolor=black!60,
  ]
    (-4,-1.7) -- (-4,-5) -- (4,-5) -- (4,-2) -- cycle;    
%Auxiliary grid
%\draw
%  (-5,-5) grid (5,1);
%\foreach \Valor in {-5,...,5}
%  \node[below] at (\Valor,-5) {\Valor};  
\end{tikzpicture}

\end{document}

enter image description here

The definition for the new pattern was inspired by Philippe Goutet's answer to Custom and built in TikZ fill patterns.

Gonzalo Medina
  • 505,128
  • Whoa! Thanks for the speedy answer. That did the job wonderfully.

    Thanks!!

    To clarify, the lower part is also a shading pattern, just more compressed, but I was able to use what you'd given me and alter it to do the job.

    I'll post the final version as an answer.

    – Ewajs Aug 29 '15 at 01:36
2

To clarify, the lower part is also a shading pattern, just more compressed, but I was able to use what you'd given me and alter it to do the job.

This is the final version made using Gonzalo Medina's answer:

\begin{figure}[!ht]
\begin{minipage}{0.5\linewidth}
\begin{tikzpicture}
\node[inner sep = 0] (a) at (-4,0) {\scalebox{-1}[1]{\includegraphics[height=2cm]{dish.png}}};
\node[inner sep = 0] (b) at (-2,0) {\scalebox{-1}[1]{\includegraphics[height=2cm]{dish.png}}};
\draw[<->] (a.south) -- node[below] {$\overrightarrow{b_1}$} (b.south) ;

\node[inner sep = 0] (a) at (-5,-5) {\scalebox{-1}[1]{\includegraphics[height=2cm]{dish.png}}};
\node[inner sep = 0] (b) at (-1,-5) {\scalebox{-1}[1]{\includegraphics[height=2cm]{dish.png}}};
\draw[<->] (a.south) -- node[below] {$\overrightarrow{b_2}$} (b.south);
\end{tikzpicture}
\end{minipage}
\begin{minipage}{0.5\linewidth}
\begin{tikzpicture}
\begin{scope}
\clip
  (0,-9) -- (7,-9) -- (7,-12.5) -- (0,-12.5) -- cycle;
\pgflowlevelobj{\pgftransformrotate{12.5}}{
\foreach \i in {-8,...,8}
  {
   \shade [overlay,shading=axis,shading angle=90, xscale = 0.3] (2*\i,-8) rectangle (2*\i+1,-14);
   \shade [overlay,shading=axis,shading angle=-90, xscale = 0.3] (2*\i+1,-8) rectangle (2*\i+2,-14);
  }
}
\end{scope}
\begin{scope}
\clip
  (0,-12.5) -- (7,-12.5) -- (7,-16) -- (0,-16) -- cycle;
  \pgflowlevelobj{\pgftransformrotate{-12.5}}{
\foreach \i in {0,...,55}
  {
   \shade [overlay,shading=axis,shading angle=-90, xscale = 0.1] (2*\i,-10) rectangle (2*\i+1,-20);
   \shade [overlay,shading=axis,shading angle=90, xscale = 0.1] (2*\i+1,-10) rectangle (2*\i+2,-20);
  }
}
\end{scope}
\end{tikzpicture}
\end{minipage}
\label{fig:titulo}
\caption{caption}
\end{figure}
Ewajs
  • 255
  • 1
  • 8
  • Since you've accepted an answer, there is no need for a duplicate answer unless you specify what is the new with your new answer. – CroCo Aug 29 '15 at 08:23
  • 1
    Indeed there's something different in my answer, I clarified that a bit now. Is that OK? – Ewajs Aug 29 '15 at 15:18