0

To draw a filled grid, I used the first code (from this post).

I managed to reduce the code a little bit by using the second code, utilizing \foreach.

Is it possible to simplify it further by filling a group of 9 squares in the grid, for example coordinates from point 0,0 to point 3,3, to be filled with green colour, and so on.

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\begin{document}
\tikzset{%
filrd/.style={%
fill=red},
filgr/.style={%
fill=green},
filyw/.style={%
fill=yellow},
filbl/.style={%
fill=blue},
}
%begin defining grid ==========================
\newcommand\sqw{1}
\tikzset{
pics/square/.default={\sqw},
pics/square/.style = {
code = {
\draw[pic actions, draw=none] (0,0) rectangle (#1,#1);
}}}%draw=none is to avoid displaying the little squares outline
\renewcommand\sqw{1.3}
%end defining grid ==========================
\begin{center}
\begin{tikzpicture}
\draw[step=\sqw] (\sqw,\sqw) grid (10*\sqw, 16*\sqw);
%col1
\pic[filgr] at (1*\sqw,15*\sqw) {square};
\pic[filgr] at (1*\sqw,14*\sqw) {square};
\pic[filgr] at (1*\sqw,13*\sqw) {square};
\pic[filyw] at (1*\sqw,12*\sqw) {square};
\pic[filyw] at (1*\sqw,11*\sqw) {square};
\pic[filyw] at (1*\sqw,10*\sqw) {square};
\pic[filbl] at (1*\sqw,9*\sqw) {square};
\pic[filbl] at (1*\sqw,8*\sqw) {square};
\pic[filbl] at (1*\sqw,7*\sqw) {square};
\pic[filgr] at (1*\sqw,6*\sqw) {square};
\pic[filgr] at (1*\sqw,5*\sqw) {square};
\pic[filgr] at (1*\sqw,4*\sqw) {square};
\pic[filrd] at (1*\sqw,3*\sqw) {square};
\pic[filrd] at (1*\sqw,2*\sqw) {square};
\pic[filrd] at (1*\sqw,1*\sqw) {square};
%col2
\pic[filgr] at (2*\sqw,15*\sqw) {square};
\pic[filgr] at (2*\sqw,14*\sqw) {square};
\pic[filgr] at (2*\sqw,13*\sqw) {square};
\pic[filyw] at (2*\sqw,12*\sqw) {square};
\pic[filyw] at (2*\sqw,11*\sqw) {square};
\pic[filyw] at (2*\sqw,10*\sqw) {square};
\pic[filbl] at (2*\sqw,9*\sqw) {square};
\pic[filbl] at (2*\sqw,8*\sqw) {square};
\pic[filbl] at (2*\sqw,7*\sqw) {square};
\pic[filgr] at (2*\sqw,6*\sqw) {square};
\pic[filgr] at (2*\sqw,5*\sqw) {square};
\pic[filgr] at (2*\sqw,4*\sqw) {square};
\pic[filrd] at (2*\sqw,3*\sqw) {square};
\pic[filrd] at (2*\sqw,2*\sqw) {square};
\pic[filrd] at (2*\sqw,1*\sqw) {square};
%col3
\pic[filgr] at (3*\sqw,15*\sqw) {square};
\pic[filgr] at (3*\sqw,14*\sqw) {square};
\pic[filgr] at (3*\sqw,13*\sqw) {square};
\pic[filyw] at (3*\sqw,12*\sqw) {square};
\pic[filyw] at (3*\sqw,11*\sqw) {square};
\pic[filyw] at (3*\sqw,10*\sqw) {square};
\pic[filbl] at (3*\sqw,9*\sqw) {square};
\pic[filbl] at (3*\sqw,8*\sqw) {square};
\pic[filbl] at (3*\sqw,7*\sqw) {square};
\pic[filgr] at (3*\sqw,6*\sqw) {square};
\pic[filgr] at (3*\sqw,5*\sqw) {square};
\pic[filgr] at (3*\sqw,4*\sqw) {square};
\pic[filrd] at (3*\sqw,3*\sqw) {square};
\pic[filrd] at (3*\sqw,2*\sqw) {square};
\pic[filrd] at (3*\sqw,1*\sqw) {square};
%col4
\pic[filrd] at (4*\sqw,15*\sqw) {square};
\pic[filrd] at (4*\sqw,14*\sqw) {square};
\pic[filrd] at (4*\sqw,13*\sqw) {square};
\pic[filbl] at (4*\sqw,12*\sqw) {square};
\pic[filbl] at (4*\sqw,11*\sqw) {square};
\pic[filbl] at (4*\sqw,10*\sqw) {square};
\pic[filyw] at (4*\sqw,9*\sqw) {square};
\pic[filyw] at (4*\sqw,8*\sqw) {square};
\pic[filyw] at (4*\sqw,7*\sqw) {square};
\pic[filbl] at (4*\sqw,6*\sqw) {square};
\pic[filbl] at (4*\sqw,5*\sqw) {square};
\pic[filbl] at (4*\sqw,4*\sqw) {square};
\pic[filgr] at (4*\sqw,3*\sqw) {square};
\pic[filgr] at (4*\sqw,2*\sqw) {square};
\pic[filgr] at (4*\sqw,1*\sqw) {square};
%col5
\pic[filrd] at (5*\sqw,15*\sqw) {square};
\pic[filrd] at (5*\sqw,14*\sqw) {square};
\pic[filrd] at (5*\sqw,13*\sqw) {square};
\pic[filbl] at (5*\sqw,12*\sqw) {square};
\pic[filbl] at (5*\sqw,11*\sqw) {square};
\pic[filbl] at (5*\sqw,10*\sqw) {square};
\pic[filyw] at (5*\sqw,9*\sqw) {square};
\pic[filyw] at (5*\sqw,8*\sqw) {square};
\pic[filyw] at (5*\sqw,7*\sqw) {square};
\pic[filbl] at (5*\sqw,6*\sqw) {square};
\pic[filbl] at (5*\sqw,5*\sqw) {square};
\pic[filbl] at (5*\sqw,4*\sqw) {square};
\pic[filgr] at (5*\sqw,3*\sqw) {square};
\pic[filgr] at (5*\sqw,2*\sqw) {square};
\pic[filgr] at (5*\sqw,1*\sqw) {square};
%col6
\pic[filrd] at (6*\sqw,15*\sqw) {square};
\pic[filrd] at (6*\sqw,14*\sqw) {square};
\pic[filrd] at (6*\sqw,13*\sqw) {square};
\pic[filbl] at (6*\sqw,12*\sqw) {square};
\pic[filbl] at (6*\sqw,11*\sqw) {square};
\pic[filbl] at (6*\sqw,10*\sqw) {square};
\pic[filyw] at (6*\sqw,9*\sqw) {square};
\pic[filyw] at (6*\sqw,8*\sqw) {square};
\pic[filyw] at (6*\sqw,7*\sqw) {square};
\pic[filbl] at (6*\sqw,6*\sqw) {square};
\pic[filbl] at (6*\sqw,5*\sqw) {square};
\pic[filbl] at (6*\sqw,4*\sqw) {square};
\pic[filgr] at (6*\sqw,3*\sqw) {square};
\pic[filgr] at (6*\sqw,2*\sqw) {square};
\pic[filgr] at (6*\sqw,1*\sqw) {square};
%col7
\pic[filyw] at (7*\sqw,15*\sqw) {square};
\pic[filyw] at (7*\sqw,14*\sqw) {square};
\pic[filyw] at (7*\sqw,13*\sqw) {square};
\pic[filgr] at (7*\sqw,12*\sqw) {square};
\pic[filgr] at (7*\sqw,11*\sqw) {square};
\pic[filgr] at (7*\sqw,10*\sqw) {square};
\pic[filrd] at (7*\sqw,9*\sqw) {square};
\pic[filrd] at (7*\sqw,8*\sqw) {square};
\pic[filrd] at (7*\sqw,7*\sqw) {square};
\pic[filyw] at (7*\sqw,6*\sqw) {square};
\pic[filyw] at (7*\sqw,5*\sqw) {square};
\pic[filyw] at (7*\sqw,4*\sqw) {square};
\pic[filbl] at (7*\sqw,3*\sqw) {square};
\pic[filbl] at (7*\sqw,2*\sqw) {square};
\pic[filbl] at (7*\sqw,1*\sqw) {square};
%col8
\pic[filyw] at (8*\sqw,15*\sqw) {square};
\pic[filyw] at (8*\sqw,14*\sqw) {square};
\pic[filyw] at (8*\sqw,13*\sqw) {square};
\pic[filgr] at (8*\sqw,12*\sqw) {square};
\pic[filgr] at (8*\sqw,11*\sqw) {square};
\pic[filgr] at (8*\sqw,10*\sqw) {square};
\pic[filrd] at (8*\sqw,9*\sqw) {square};
\pic[filrd] at (8*\sqw,8*\sqw) {square};
\pic[filrd] at (8*\sqw,7*\sqw) {square};
\pic[filyw] at (8*\sqw,6*\sqw) {square};
\pic[filyw] at (8*\sqw,5*\sqw) {square};
\pic[filyw] at (8*\sqw,4*\sqw) {square};
\pic[filbl] at (8*\sqw,3*\sqw) {square};
\pic[filbl] at (8*\sqw,2*\sqw) {square};
\pic[filbl] at (8*\sqw,1*\sqw) {square};
%col9
\pic[filyw] at (9*\sqw,15*\sqw) {square};
\pic[filyw] at (9*\sqw,14*\sqw) {square};
\pic[filyw] at (9*\sqw,13*\sqw) {square};
\pic[filgr] at (9*\sqw,12*\sqw) {square};
\pic[filgr] at (9*\sqw,11*\sqw) {square};
\pic[filgr] at (9*\sqw,10*\sqw) {square};
\pic[filrd] at (9*\sqw,9*\sqw) {square};
\pic[filrd] at (9*\sqw,8*\sqw) {square};
\pic[filrd] at (9*\sqw,7*\sqw) {square};
\pic[filyw] at (9*\sqw,6*\sqw) {square};
\pic[filyw] at (9*\sqw,5*\sqw) {square};
\pic[filyw] at (9*\sqw,4*\sqw) {square};
\pic[filbl] at (9*\sqw,3*\sqw) {square};
\pic[filbl] at (9*\sqw,2*\sqw) {square};
\pic[filbl] at (9*\sqw,1*\sqw) {square};
\end{tikzpicture}
\end{center}

\begin{center}
\begin{tikzpicture}
\draw[step=\sqw] (\sqw,\sqw) grid (10*\sqw, 16*\sqw);
%gr
\foreach \i in {15,14,13,6,5,4}{ 
\foreach \j in {1,2,3}{ 
\pic[filgr] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {18,17,16,3,2,1}{ 
\foreach \j in {4,5,6}{ 
\pic[filgr] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {21,20,19,12,11,10}{ 
\foreach \j in {7,8,9}{ 
\pic[filgr] at (\j*\sqw,\i*\sqw) {square};};};
%yw
\foreach \i in {21,20,19,12,11,10}{ 
\foreach \j in {1,2,3}{ 
\pic[filyw] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {9,8,7}{ 
\foreach \j in {4,5,6}{ 
\pic[filyw] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {15,14,13,6,5,4}{ 
\foreach \j in {7,8,9}{ 
\pic[filyw] at (\j*\sqw,\i*\sqw) {square};};};
%bl
\foreach \i in {18,17,16,9,8,7}{ 
\foreach \j in {1,2,3}{ 
\pic[filbl] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {21,20,19,12,11,10,6,5,4}{ 
\foreach \j in {4,5,6}{ 
\pic[filbl] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {3,2,1}{ 
\foreach \j in {7,8,9}{ 
\pic[filbl] at (\j*\sqw,\i*\sqw) {square};};};
%rd
\foreach \i in {3,2,1}{ 
\foreach \j in {1,2,3}{ 
\pic[filrd] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {15,14,13}{ 
\foreach \j in {4,5,6}{ 
\pic[filrd] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {18,17,16,9,8,7}{ 
\foreach \j in {7,8,9}{ 
\pic[filrd] at (\j*\sqw,\i*\sqw) {square};};};
%
\end{tikzpicture}
\end{center}
\end{document}

enter image description here

1 Answer 1

1

I don't know your goal, so I don't know either if this is the right approach. It's very simple, though. It only needs a \foreach that loops over the colors. Then I apply the dimension \sqw as a scale factor instead of applying it in each element. I suppose, seen the link you provided, that you need more drawings over this grid. If the scale is not appropriate for them just put all my code inside a scope and apply the scale to this scope.

For example:

\documentclass[tikz,border=2mm]{standalone}

\definecolor{colorg}{HTML}{00FF00} % green
\definecolor{colorr}{HTML}{FF0000} % red
\definecolor{colory}{HTML}{FFFF00} % yellow
\definecolor{colorb}{HTML}{0000FF} % blue

\begin{document}
\newcommand\sqw{1}
\begin{tikzpicture}[scale=\sqw,y=-1cm]
\foreach[count=\ii]\i in {g,r,y,
                          y,b,g,
                          b,y,r,
                          g,b,y,
                          r,g,b}
{
  \pgfmathsetmacro\myrow{3*mod(\ii-1,3)}
  \pgfmathsetmacro\mycol{3*div(\ii-1,3)}
  \fill[color\i] (\myrow,\mycol) rectangle ++ (3,3);
}
\draw (0,0) grid (9,15);
\end{tikzpicture}
\end{document}

enter image description here

Edit: This is what I had in mind, but as I said, I don't know if it is the better approach:

\documentclass[tikz,border=2mm]{standalone}

\definecolor{colorg}{HTML}{00FF00} % green
\definecolor{colorr}{HTML}{FF0000} % red
\definecolor{colory}{HTML}{FFFF00} % yellow
\definecolor{colorb}{HTML}{0000FF} % blue
\definecolor{colorw}{HTML}{FFFFFF} % white

\begin{document}
\newcommand\sqw{1}
\begin{tikzpicture}[scale=\sqw,y=-1cm]
\foreach[count=\ii]\i in {g,r,y,
                          y,b,g,
                          b,y,r,
                          g,b,y,
                          r,g,b,
                          w,w,y}
{
  \pgfmathsetmacro\myrow{3*mod(\ii-1,3)}
  \pgfmathsetmacro\mycol{3*div(\ii-1,3)}
  \fill[color\i] (\myrow,\mycol) rectangle ++ (3,3);
}
\draw (0,0) grid (9,18);

\foreach\x/\y in {0/15,1/17,2/17} 
  \draw[very thick,fill=magenta] (\x+0.5,\y+0.5) circle [radius=0.3cm]; % <-- here you can define a \pic
\foreach\x/\y in {3/15,4/16,4/17,3/17}
  \draw[very thick,fill=brown] (\x+0.2,\y+0.2) rectangle ++ (0.6,0.6);  % <-- here you can define another \pic
\end{tikzpicture}
\end{document}

enter image description here

2
  • Thank you for your answer. Some grid cells are filled with other shapes and graphics. So, unfortunately your solution is not applicable for my entire drawing which involve other elements. So, is it possible, in my code, to use filling a group of squares as one line. For example from grid point 0,0 to 3,3.
    – Hany
    Jan 12 at 7:26
  • Thank you for your update. But is it possible, in my code, to use filling a group of squares in a one command line. For example from grid point 0,0 to 3,3.
    – Hany
    Jan 12 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.