8

I have got a grid which looks like this

\tdplotsetmaincoords{60}{125}
\begin{tikzpicture}
    [tdplot_main_coords,
        grid/.style={very thin,gray}]

\foreach \x in {-2.5,-2.25,...,2.5}
    \foreach \y in {-2.5,-2.25,...,2.5}
    {
        \draw[grid] (\x,-2.5) -- (\x,2.5);
        \draw[grid] (-2.5,\y) -- (2.5,\y);
    }
\end{tikzpicture}

Thats 10x10 cells. How can I fill the cells with color? For example, fill some of the cells with red so that the colored cells form the shape of a circle.

EDIT: Above I said, "Thats 10x10 cells". In fact, its 20x20 :)

2
  • For example, fill some of the cells with red so that the colored cells form the shape of a circle. Are you trying to carry out "rasterization"
    – CroCo
    Oct 7, 2015 at 11:01
  • No, I am just interested in how to fill one of those grid cells with color Oct 7, 2015 at 11:10

2 Answers 2

11

To fill a particular cell with color, you need to draw a "cube" (in fact, a cube face), for example:

    \fill[cube, yourcolor] (\x,\y) -- +(0, .25) -- +(.25, .25) -- +(.25,0) -- cycle;

To draw arbitrary "bitmaps" on in your grid, I think the best approach is to have the bitmap defined in a bidimensional array, and use pgfmath to access the elements of that array. For example:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{tikz-3dplot}

\def\pixelmap{{%
{0,0,1,1,1,1,0,0,0},
{0,1,0,0,0,0,1,0,0},
{1,0,1,0,1,0,0,1,0},
{1,0,1,0,1,0,0,1,0},
{1,0,0,0,0,1,0,1,0},
{1,0,1,1,1,0,0,1,0},
{0,1,0,0,0,0,1,0,0},
{0,0,1,1,1,1,0,0,0},
{0,0,0,0,0,0,0,0,0}
}}

\colorlet{color0}{yellow!40}
\colorlet{color1}{red}

\begin{document}
\tdplotsetmaincoords{60}{125}
\begin{tikzpicture}
    [tdplot_main_coords,
        grid/.style={very thin,gray},
    ]

\foreach \x [count=\i from 0] in {-2.5,-2.25,...,2.5}
    \foreach \y [count=\j from 0] in {-2.5,-2.25,...,2.5}
    {
        \def\bit{0}
            \pgfmathsetmacro{\bit}{\pixelmap[mod(\i,9)][mod(\j,9)]}
        \draw[grid] (\x,-2.5) -- (\x,2.5);
        \draw[grid] (-2.5,\y) -- (2.5,\y);
        \fill[cube, color\bit] (\x,\y) -- +(0, 0.25) -- +(.25, .25) -- +(.25,0) -- cycle;
    }
\end{tikzpicture}
\end{document}

This produces the result:

Result

(Note that, since my bitmap is only 9x9 while your grid is 20x20, I used mod function to repeat the pattern)

3
  • Just one thing: can you remove the styles for cube and axis? I posted them accidently (my real setup has more going on). Just so that other people don't get confused :) Oct 7, 2015 at 11:36
  • @user2426316 done
    – JLDiaz
    Oct 7, 2015 at 11:52
  • are you missing something like cube/.style={very thick,black}?
    – PatrickT
    Jun 18, 2016 at 19:12
22

Grid is nothing but a bunch of aligned squares. For 2D case,

\documentclass[border={10pt}]{standalone}

\usepackage{tikz,}

\begin{document}

\begin{tikzpicture}
    [%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        box/.style={rectangle,draw=black,thick, minimum size=1cm},
    ]%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\foreach \x in {0,1,...,10}{
    \foreach \y in {0,1,...,10}
        \node[box] at (\x,\y){};
}

\node[box,fill=green] at (8,8){};  
\node[box,fill=red  ] at (5,5){};  
\node[box,fill=blue ] at (2,2){};  

\end{tikzpicture}

\end{document}

enter image description here

For 3D case,

    \documentclass[border={10}]{standalone}
\usepackage{tikz}  
\usepackage{tikz-3dplot} 

\tdplotsetmaincoords{60}{125} % view angles
\tdplotsetrotatedcoords{0}{0}{0} 
\begin{document}

\begin{tikzpicture}
    [%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        scale=5,tdplot_rotated_coords,
        grid/.style={very thin,gray}
    ]%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %draw a grid in the x-y plane
    \foreach \x in {0,1,...,10}
        \foreach \y in {0,1,...,10}
        {
            \draw[grid] (\x,0) -- (\x,10);
            \draw[grid] (0,\y) -- (10,\y);
        };
    \draw[fill=blue]  (0,0,0) -- (0,1,0) -- (1,1,0) -- (1,0,0) -- cycle;
    \draw[fill=red ]  (1,1,0) -- (2,1,0) -- (2,2,0) -- (1,2,0) -- cycle;

\end{tikzpicture}    
\end{document}

enter image description here

You must log in to answer this question.

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