How can I tikz the following?

There is a grid of arbitrary size. (But it always has the same width and height.) There is one position selected in the beginning. We start coloring the tiles adjacent to that position. After that we color the tiles that were not colored before and are adjacent to a tile that was already colored. We repeat this process until all tiles are colored. In the beginning the color should be really transparent and become less transparent with the repetitions.

Here are two examples how this could look like:

Here's the code for the grid:





    % Draw the grid
    \draw[thick,step=1cm,color=black!50] (0,0) grid (\size,\size);

    % Draw border
    \draw[ultra thick] (0,0) -- (\size,0) -- (\size,\size) -- (0,\size) -- (0,0) -- (\size,0);



I'd guess that it is a lot easier if the starting point is in a corner like in the first picture. So if you could only come up with a solution with this constraint, I'd be still interested in it.

1 Answer 1


Something like this? The position of the brightest square has the keys x and y, the size size and the color color.

\begin{tikzpicture}[pics/descending grid/.style={code={
    \tikzset{descending grid/.cd,#1}
    \def\pv##1{\pgfkeysvalueof{/tikz/descending grid/##1}}%
    \foreach \XX in {1,...,\pv{size}}
     {\foreach \YY in {1,...,\pv{size}}
      \draw[fill=gcolor,fill opacity=\myopa] (\XX-1-\pv{size}/2,\YY-1-\pv{size}/2)
       rectangle ++ (1,1);
    \draw[thick] (-\pv{size}/2,-\pv{size}/2) rectangle (\pv{size}/2,\pv{size}/2);
    }},descending grid/.cd,
 \path (0,0) pic{descending grid}
 (7,0) pic{descending grid={size=4,x=3,y=2}}
 (0,-5) pic{descending grid={size=4,x=2,y=4,color=red}}  ;  

enter image description here

  • @Schodinger's cat: It's nice way of coding like pic{descending grid={size=4,x=2,y=4,color=red} with initial (default) value. In spiring of TikZ's syntax, that is, TikZ understand color=red even we write just red, can we do that in this kind of situation: writing pic{descending grid={size=4,x=2,y=4,red} in stead of pic{descending grid={size=4,x=2,y=4,color=red} ?
    – Black Mild
    Apr 13, 2020 at 1:25
  • 1
    @BlackMild The answer is yes, with the /.try key, but it is IMHO not worth the effort. The more so I like the fact that one can use the already taken keys in a subdirectory without problems. Basically, if you do the /.try story, you need major efforts to make x and y work. However, as this thingy changes only the fill opacity, one could admit a fill color from outside, i.e. \pic[fill=red]{descending grid={size=4,x=2,y=4}}; could be made work, but is no shorter syntax.
    – user194703
    Apr 13, 2020 at 1:32
  • cat: thank you for clarifying!
    – Black Mild
    Apr 13, 2020 at 1:44
  • I guess this way the library angles did!
    – Black Mild
    Apr 13, 2020 at 16:58
  • 1
    @BlackMild Please note that there is the pic actions key that allows you to do a lot of things. It is also implicitly used (and you also inherit ambient pgf keys), so ` \path (0,0) pic[dashed]{descending grid};` gives you locally dashed edges.
    – user194703
    Apr 13, 2020 at 19:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .