# Divide a rectangle into n times k equal cells

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:

or

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

a rectangle like this:

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:

Is sufficient to give each colored cell the same color.

-
I am pretty sure the answer to your question is "Yes", but I am not sure I fully understand. If you divide a rectangle wouldn't you reuire two coordinates to define a cell? And what do you mean by "which should get a colored background?" Although it may seem trivial you should compose a compilable MWE including the \documentclass and the appropriate packages to show what you have tried so far, even it is not working. – Peter Grill Nov 23 '11 at 16:37
Questions are strong in this one... I think it is better to chop things into byte sized versions. My two immediate concerns are : 1) Give a list means, you can retrieve the coordinates of any cell or explicitly stating on the rectangle? 2) colored background means like a checkerboard or to be colored later individually? – percusse Nov 23 '11 at 16:39
After 3 very successful attempts to accommodate your request, you updated your question with a more detailed description. For future reference, I find that the following requires some emphasis: Always keep the answerer in mind when asking a question since they can't read your mind, regardless of how trivial things may sound. – Werner Nov 23 '11 at 17:58

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:

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.

-
@'wh1t3' Thanks, that's exactly what I want. Could you point out, how to adjust this such that the first #3 is the rectangle start coordinate but such that #4 is the width and height of the rectangle? – student Nov 23 '11 at 17:47

As I said in my comments it is not clear what you are looking for, and perhaps the solution by @Altermundus is what you are looking for. But here is a way to obtain the coordinates that divide a rectangle into a specific number of divisions in either direction given by: \NumOfXDivisions and \NumOfYDivisions:

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

% Define the coordinates for the rectangle
\newcommand*{\xmin}{0}%
\newcommand*{\xmax}{10}%
\newcommand*{\ymin}{0}%
\newcommand*{\ymax}{5}%

\newcommand*{\NumOfXDivisions}{2}%
\newcommand*{\NumOfYDivisions}{5}%

\begin{document}
\begin{tikzpicture}
\draw[red] (\xmin,\ymin)
-- (\xmax,\ymin)
-- (\xmax,\ymax)
-- (\xmin,\ymax)
-- cycle;

\pgfmathsetmacro{\xStepSize}{(\xmax-\xmin)/\NumOfXDivisions}
\pgfmathsetmacro{\yStepSize}{(\ymax-\ymin)/\NumOfYDivisions}

\foreach \i in {0,...,\NumOfXDivisions}{
\foreach \j in {0,...,\NumOfYDivisions}{
\pgfmathsetmacro{\xValue}{\i*\xStepSize+\xmin}
\pgfmathsetmacro{\yValue}{\j*\yStepSize+\ymin}
\fill (\xValue,\yValue) circle (2pt);
}
}
\end{tikzpicture}

\bigskip\noindent
The coordinates are:
\pgfmathsetmacro{\xStepSize}{(\xmax-\xmin)/\NumOfXDivisions}
\pgfmathsetmacro{\yStepSize}{(\ymax-\ymin)/\NumOfYDivisions}

\foreach \i in {0,...,\NumOfXDivisions}{
\foreach \j in {0,...,\NumOfYDivisions}{
\pgfmathsetmacro{\xValue}{\i*\xStepSize+\xmin}
\pgfmathsetmacro{\yValue}{\j*\yStepSize+\ymin}
(\xValue,\yValue);
}
}
\end{document}

-

You can use \usetikzlibrary{shapes.multipart}

Example from pgfmanual:

\begin{tikzpicture}
\tikzset{every node/.style={rectangle split, draw, minimum width=.5cm}}
\node[rectangle split part fill={red!50, green!50, blue!50, yellow!50}]  {};
\node[rectangle split part fill={red!50, green!50, blue!50}] at (0.75,0) {};
\node[rectangle split part fill={red!50, green!50}]          at (1.5,0)  {};
\node[rectangle split part fill={red!50}]                    at (2.25,0) {};
\end{tikzpicture}


You can access each rectangle without problem (see the manual)

-