6

I'm trying to create a "3D" schema of a grid where every "pixel" is colored with a random shade of blue (or whatever other color). As a tikz beginner, I'm already glad that I managed to tilt the grid, but so far I only managed to change the color for the whole grid. This is my MWE:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{xcolor}
\begin{document}
    \begin{tikzpicture}[every node/.style={minimum size=1cm}]
        \begin{scope}[
            every node/.append style={
                yslant=0.5, xslant=-1}, yslant=0.5, xslant=-1
            ]
            \fill[blue, fill opacity=.6] (0,0) rectangle (5.5, 5.5);
            \draw[gray, very thick] (0,0) rectangle (5.5, 5.5);
            \draw[step=5mm, gray] (0,0) grid (5.5, 5.5);
        \end{scope}
    \end{tikzpicture}
\end{document}

From what I saw in other posts, I fear I need to draw every pixel on its own?

1
  • 1
    How to graph a color function relationship shows a case of coloring each point in the grid with a custom color. You should be able to use that and modify \ComputeColor to choose the desried random color. If that works for you, then this can be closed as a dupliacte of that question. Commented Dec 17, 2021 at 8:43

1 Answer 1

14

You can use two nested \foreach sentences for rows and columns and compute inside the loops a random number for each element in the grid.

I changed your approach for the 3d view, using 3d and perspective TikZ libraries, but the \foreach method is also valid with your example.

For example,

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{3d,perspective}

\begin{document}
\begin{tikzpicture}[isometric view, canvas is xy plane at z=0]
  \foreach\i in {0,0.5,...,5} \foreach\j in {0,0.5,...,5}
    \pgfmathparse{100*rnd}
    \fill[blue!\pgfmathresult,draw=gray] (\i,\j) rectangle (\i+0.5,\j+0.5);
  \draw[gray, very thick] (0,0,0) rectangle (5.5, 5.5,0);
\end{tikzpicture}
\end{document}

enter image description here

2
  • This works like a charm, but do I understand correctly that the whole canvas is now in 3D? Because when I tried to place additional elements, I had a really hard time aligning them (although I managed it somehow). Is there a simpler way of doing that? Full code available here: https://www.overleaf.com/read/jsjwzrhswrgb
    – s6hebern
    Commented Dec 21, 2021 at 10:10
  • 1
    @s6hebern, yes you can place all the code inside a scope with the 3d options: \begin{scope}[isometric view, canvas is xy plane at z=0]...\end{scope}. This way all you draw outside the scope will not be in 3d. Commented Dec 21, 2021 at 10:18

You must log in to answer this question.

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