[Tex/LaTex] Divide a rectangle into n times k equal cells

tikz-pgf

Is there a way to divide a given rectangle in tikz into n times k equal cells and give a list of coordinates (n_i,k_i) of cells, which should get a colored background?

Edit:

I think I should describe a bit more explicit, what I want:

I want a function \myrec such that for example:

\myrec{(0,0),(4.2,6.7),(3,1)}

draws a rectangle from starting point (0,0) with height=4.2 and width=6.7 which is equally divided like this:

Rectangle 3x1

or

\myrec{(0,0),(4.2,6.7),(3,2)}

a rectangle like this:

Rectangle 3x2

Now lets count the cells in such a divided rectangle from the corner on the left below, then I want to be able to give the (i,k)'s cell some color,

for example

\myrec[(1,2),(3,1),blue]{(0,0),(4.2,6.7),(3,2)}

should give something like this:

Rectangle 3x2 colored

Is sufficient to give each colored cell the same color.

Best Answer

The following assumes you want to draw a rectangle, give two numbers (n and k) for the number of divisions in x and y direction and a list of 'coordinates' defining which subdivisions should be filled. The following code achieves this:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
  \def\rectDiv#1#2#3#4#5{%#columns, #rows, rectangle start, rectangle end, list of elements to fill
    \begin{tikzpicture}
      \draw #3 rectangle #4;
      \path #3;
      \pgfgetlastxy{\firstx}{\firsty}
      \path #4;
      \pgfgetlastxy{\secondx}{\secondy}
      \pgfmathsetlengthmacro{\xdiff}{\secondx-\firstx}
      \pgfmathsetlengthmacro{\ydiff}{\secondy-\firsty}
      \pgfmathsetlengthmacro{\myxstep}{\xdiff/#1}
      \pgfmathsetlengthmacro{\myystep}{\ydiff/#2}
      \foreach \x in {1,...,#1}{
        \draw ($#3 +\x*(\myxstep,0)$) -- ($#3 +(0,\ydiff) +\x*(\myxstep,0)$);
      }
      \foreach \y in {1,...,#2}{
        \draw ($#3 +\y*(0,\myystep)$) -- ($#3 +(\xdiff,0) +\y*(0,\myystep)$);
      }
      \foreach \i/\j in {#5}{
        \path[fill=blue!20,draw] ($#3 + (\i*\myxstep,\j*\myystep)$) rectangle ($#3 + (\i*\myxstep,\j*\myystep) + (\myxstep,\myystep)$);
      }
    \end{tikzpicture}
  }
\begin{document}
  \rectDiv{7}{5}{(1,1)}{(4,3)}{0/0,1/1,2/0,5/3}
\end{document}

The parameters are as follows:

  1. Number of columns
  2. Number of rows
  3. Rectangle start coordinate
  4. Rectangle end coordinate
  5. List of index pairs to be filled

The list of index pairs is to be given in an i/j fashion. Where the box denoted (n_i,k_j) is then filled. Using your notation.

You could potentially change it such that you only specify the endpoints of the rectangle, assuming it starts at (0,0). The indexing of the subdivisions starts at 0. The result is the following:

TikZ subdivision

Update: After comment. It's quite easy to modify #4 to be (width, height) instead of the end coordinate. Since the end coordinate is simply (start) + (width,height). This can cause some problems in the path and using \pgfgetlastxy though and therefore we also define an extra coordinate. The code can be modified by replacing

\draw #3 rectangle #4;
\path #3;
\pgfgetlastxy{\firstx}{\firsty}
\path #4;

with

\draw #3 rectangle ($#3 + #4$) coordinate (end);
\path #3;
\pgfgetlastxy{\firstx}{\firsty}
\path (end);

Changing the example to

\rectDiv{7}{5}{(1,1)}{(3,2)}{0/0,1/1,2/0,5/3}

yields the exact same result. Note that you have to specify (width,height) and not (height,width). This is far easier, because it allows for the simple addition.