# Adding 3D elements to a grid in Tikz

Based on my question here I created the the triangular mesh given below. Note that I am very new to Tikz so I don't know if this is the best approach.

I would like to add 2 to 3 3D elements to the grid, basically placing cubes (of different sizes) on top of the grid with an isometric view so both the grid and cubes are in isometric view (so the grid displays similar to one segment of this example. It is important that the vertices of the cube don't intersect with the vertices or edges of the already-created mesh.

Then I would like to add the x-y coordinates of the cubes to a mesh, creating a separate plot above the one with the cubes, showing a 2D "plan" view of the domain.

Stylistically, I like the cone on the left side in this example, but I'm not sure how to recreate something similar with cubes in Tikz.

Is this possible, and is there a MWE available somewhere?

\documentclass[tikz,border=5]{standalone}
\begin{document}

\begin{tikzpicture}
\foreach \i [evaluate={\ii=int(\i-1);}] in {0,...,2}{
\foreach \j [evaluate={\jj=int(\j-1);}] in {0,...,2}{
\coordinate [shift={(\j,\i)}] (n-\i-\j) at (0:1);
\ifnum\i>0
\draw [help lines] (n-\i-\j) -- (n-\ii-\j);
\fi
\ifnum\j>0
\draw [help lines] (n-\i-\j) -- (n-\i-\jj);
\ifnum\i>0
\pgfmathparse{int(rnd>.5)}
\ifnum\pgfmathresult=0
\draw [help lines] (n-\i-\j) -- (n-\ii-\jj);
\else%
\draw [help lines] (n-\ii-\j) -- (n-\i-\jj);
\fi%
\fi
\fi
}}
\end{tikzpicture}


## Update

Here's another option producing an isometric view; the squares in the top layer can be easily labelled since the four corners can be accessed using (t-#), (l-#), (b-#), and (r-#) (see code below):

The code:

\documentclass[tikz,border=5]{standalone}

\def\Side{0.5cm}
\newcounter{mycube}

\tikzset{
cube/.style={
thin,
fill opacity=.5,
fill=cyan!40,
line join=round
}
}

\newcommand\Cube[1][1]{%
\begin{tikzpicture}[scale=#1]
\filldraw[cube]
(0,0) --
++(30:\Side) coordinate (lr) --
++(0,\Side) --
++(210:\Side) --
cycle --
++(150:\Side) --
++(0,\Side) --
++(-30:\Side) --
cycle;
\filldraw[cube]
(0,\Side) --
++(30:\Side) --
++(150:\Side) coordinate (up) --
++(210:\Side) --
cycle;
\draw[dashed,help lines]
(up) -- (0,\Side);
\draw[dashed,help lines]
(lr) -- ++(150:\Side) -- ++(210:\Side);
\end{tikzpicture}%
}

\newcommand\BorderCube[1][1]{%
\stepcounter{mycube}%
\begin{tikzpicture}[scale=#1,remember picture]
\filldraw[cube]
(0,0) coordinate (b-\themycube) --
++(30:\Side) coordinate (r-\themycube) --
++(150:\Side) coordinate (t-\themycube) --
++(210:\Side) coordinate (l-\themycube) --
cycle;
\end{tikzpicture}%
}

\newsavebox\myboxa
\newsavebox\myboxb
\newsavebox\myboxc
\savebox\myboxa{\Cube[0.5]}
\savebox\myboxb{\Cube[0.75]}
\savebox\myboxc{\Cube[0.35]}

\newsavebox\myboxd
\newsavebox\myboxe
\newsavebox\myboxf
\savebox\myboxd{\BorderCube[0.5]}
\savebox\myboxe{\BorderCube[0.75]}
\savebox\myboxf{\BorderCube[0.35]}

\newcommand\MyGrid{
\pgfmathsetseed{9}
\foreach \i [evaluate={\ii=int(\i-1);}] in {0,...,2}{
\foreach \j [evaluate={\jj=int(\j-1);}] in {0,...,2}{
\coordinate [shift={(\j,\i)}] (n-\i-\j) at (0:1);
\ifnum\i>0
\draw [help lines] (n-\i-\j) -- (n-\ii-\j);
\fi
\ifnum\j>0
\draw [help lines] (n-\i-\j) -- (n-\i-\jj);
\ifnum\i>0
\pgfmathparse{int(rnd>.5)}
\ifnum\pgfmathresult=0
\draw [help lines] (n-\i-\j) -- (n-\ii-\jj);
\else%
\draw [help lines] (n-\ii-\j) -- (n-\i-\jj);
\fi%
\fi
\fi
}}
}

\begin{document}

\begin{tikzpicture}[
remember picture,
y={(-1cm,0.5cm)},
x={(1cm,0.5cm)},
z={(0cm,1cm)}
]
\MyGrid
\node[overlay] at (1.65,1.35) {\usebox\myboxa};
\node[overlay] at (1.7,0.5) {\usebox\myboxb};
\node[overlay] at (2.7,1.35) {\usebox\myboxc};
\begin{scope}[shift={(0,50pt)}]
\MyGrid
\node[overlay] at (1.65,1.35) {\usebox\myboxd};
\node[overlay] at (1.7,0.5) {\usebox\myboxe};
\node[overlay] at (2.7,1.35) {\usebox\myboxf};

\node[above=-2pt,font=\tiny] at (t-2) {$a$};
\node[left=-2pt,font=\tiny] at (l-2) {$b$};
\node[below=-2pt,font=\tiny] at (b-2) {$c$};
\node[right=-2pt,font=\tiny] at (r-2) {$d$};
\end{scope}
\end{tikzpicture}

\end{document}


## First version

You can use the tikz-3dplot package:

The code:

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

\newcommand\Cube[1][0.1]{%
\tdplotsetmaincoords{60}{60}
\begin{tikzpicture}[
scale=#1,
tdplot_main_coords,
cube/.style={thin,fill opacity=.5,fill=cyan!40,line join=round}
]
%draw the bottom of the cube
\filldraw[cube] (0,0,0) -- (0,2,0) -- (2,2,0) -- (2,0,0) -- cycle;
%draw the back-right of the cube
\filldraw[cube] (0,0,0) -- (0,2,0) -- (0,2,2) -- (0,0,2) -- cycle;
%draw the back-left of the cube
%\filldraw[cube] (0,0,0) -- (2,0,0) -- (2,0,2) -- (0,0,2) -- cycle;
%draw the front-right of the cube
\filldraw[cube] (2,0,0) -- (2,2,0) -- (2,2,2) -- (2,0,2) -- cycle;
%draw the front-left of the cube
\filldraw[cube] (0,2,0) -- (2,2,0) -- (2,2,2) -- (0,2,2) -- cycle;
%draw the top of the cube
\filldraw[cube] (0,0,2) -- (0,2,2) -- (2,2,2) -- (2,0,2) -- cycle;
\end{tikzpicture}%
}

\newcommand\BorderCube[1][0.1]{%
\tdplotsetmaincoords{60}{60}
\begin{tikzpicture}[
scale=#1,
tdplot_main_coords,
cube/.style={thin,fill opacity=.5,fill=cyan!40,line join=round}
]
%draw the bottom of the cube
\filldraw[cube] (0,0,0) -- (0,2,0) -- (2,2,0) -- (2,0,0) -- cycle;
%draw/hide the top of the cube
\filldraw[opacity=0] (0,0,2) -- (0,2,2) -- (2,2,2) -- (2,0,2) -- cycle;
\end{tikzpicture}%
}

\newsavebox\myboxa
\newsavebox\myboxb
\newsavebox\myboxc
\savebox\myboxa{\Cube}
\savebox\myboxb{\Cube[0.15]}
\savebox\myboxc{\Cube[0.075]}

\newsavebox\myboxd
\newsavebox\myboxe
\newsavebox\myboxf
\savebox\myboxd{\BorderCube}
\savebox\myboxe{\BorderCube[0.15]}
\savebox\myboxf{\BorderCube[0.075]}

\newcommand\MyGrid{
\pgfmathsetseed{3}
\foreach \i [evaluate={\ii=int(\i-1);}] in {0,...,2}{
\foreach \j [evaluate={\jj=int(\j-1);}] in {0,...,2}{
\coordinate [shift={(\j,\i)}] (n-\i-\j) at (0:1);
\ifnum\i>0
\draw [help lines] (n-\i-\j) -- (n-\ii-\j);
\fi
\ifnum\j>0
\draw [help lines] (n-\i-\j) -- (n-\i-\jj);
\ifnum\i>0
\pgfmathparse{int(rnd>.5)}
\ifnum\pgfmathresult=0
\draw [help lines] (n-\i-\j) -- (n-\ii-\jj);
\else%
\draw [help lines] (n-\ii-\j) -- (n-\i-\jj);
\fi%
\fi
\fi
}}
}

\begin{document}

\tdplotsetmaincoords{60}{60}
\begin{tikzpicture}[
tdplot_main_coords,
cube/.style={very thick,black},
]
\MyGrid
\node[overlay] at (1.15,0.55) {\usebox\myboxa};
\node[overlay] at (2.35,0.28) {\usebox\myboxb};
\node[overlay] at (2.25,1.15) {\usebox\myboxc};
\begin{scope}[shift={(0,50pt)},transform shape]
\MyGrid
%\begin{scope}[shift={(0pt,0pt)}]
\node[overlay] at (1.15,0.55) {\usebox\myboxd};
\node[overlay] at (2.35,0.28) {\usebox\myboxe};
\node[overlay] at (2.25,1.15) {\usebox\myboxf};
%\end{scope}
\end{scope}
\end{tikzpicture}

\end{document}

• Cubes don't look in isometric view, do they ? – Tarass Aug 10 '15 at 16:13
• @Tarass I have no idea what that is. – Gonzalo Medina Aug 10 '15 at 16:14
• @Tarass perhaps what I just did in my edit? – Gonzalo Medina Aug 10 '15 at 16:20
• @GonzaloMedina I updated my question; I would like the grid to be in isometric view as well. – chasely Aug 10 '15 at 16:39
• @chasely Sure is possible, but why don't you ask all your requirements from the beginning? It's not nice to have to rewrite the answer each time you add a requirement. – Gonzalo Medina Aug 10 '15 at 17:35