# Representing general transformations on a 3D grid using TikZ

I have a cuboid made from unit squares which in 2D looks like the following:

Considering a 3D version of this (i.e. a cuboid). I wanted to use Tikz to demonstrate some of the possible deformations that we can introduce to this 3D grid. I wanted to show arbitrary streching (in any direction), shearing (where one half is pulled in one direction and the other half is pulled in the other direction) and some general inhomogeneous deformation (a deformation which cannot be described by any of rotations, shearing, streching etc. It is a very general transformation).

I would like each of the spheres shaded and shown in different colours for the different transformations.

• I am new to using tikz, and have still struggled in representing general transformations with this 3D grid. Any further help? – Sid Aug 26 '15 at 12:23

This is not a complete answer for your problem (i.e. and it is my first answer in this community for tikz. :)) however, once you draw the shape, you need to multiply each vertex with the transformation matrix. I've modified this code and added the spheres. The result is

For rotation around z-axis, this is the result (i.e. the rotation order zyz but you can change it, see this link.

Other transformations are simple. Just define a variable and hit it with the vertexes of the cube.

\documentclass{article}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\usepackage{amssymb}
\usepackage{xifthen}

\tdplotsetmaincoords{60}{125}
\tdplotsetrotatedcoords{0}{0}{0} %<- rotate around (z,y,z)
\begin{document}

\begin{tikzpicture}[scale=3,tdplot_rotated_coords,
cube/.style={very thick,black},
grid/.style={very thin,gray},
axis/.style={->,blue,ultra thick},
rotated axis/.style={->,purple,ultra thick}]

%draw a grid in the x-y plane
\foreach \x in {-0.5,0,...,2.5}
\foreach \y in {-0.5,0,...,2.5}
{
\draw[grid] (\x,-0.5) -- (\x,2.5);
\draw[grid] (-0.5,\y) -- (2.5,\y);
}

%draw the main coordinate frame axes
\draw[axis,tdplot_main_coords] (0,0,0) -- (3.5,0,0) node[anchor=west]{$x$};
\draw[axis,tdplot_main_coords] (0,0,0) -- (0,3.5,0) node[anchor=north west]{$y$};
\draw[axis,tdplot_main_coords] (0,0,0) -- (0,0,3.5) node[anchor=west]{$z$};

%draw the rotated coordinate frame axes
\draw[rotated axis] (0,0,0) -- (3,0,0) node[anchor=west]{$x'$};
\draw[rotated axis] (0,0,0) -- (0,3,0) node[anchor=south west]{$y'$};
\draw[rotated axis] (0,0,0) -- (0,0,3) node[anchor=west]{$z'$};

%draw the top and bottom of the cube
\draw[cube,fill=blue!5] (0,0,0) -- (0,2,0) -- (2,2,0) -- (2,0,0) -- cycle;

%draw the top and bottom of the cube
\draw[cube,fill=red!5] (0,0,0) -- (0,2,0) -- (0,2,2) -- (0,0,2) -- cycle;

%draw the top and bottom of the cube
\draw[cube,fill=green!5] (0,0,0) -- (2,0,0) -- (2,0,2) -- (0,0,2) -- cycle;

\foreach \x in {0,1,2}
\foreach \y in {0,1,2}
\foreach \z in {0,1,2}{
%#####################################################
\ifthenelse{  \lengthtest{\x pt < 2pt}  }
{
% True
\draw [black]   (\x,\y,\z) -- (\x+1,\y,\z);
}
{% False
}
%#####################################################
\ifthenelse{  \lengthtest{\y pt < 2pt}  }
{
% True
\draw [black]   (\x,\y,\z) -- (\x,\y+1,\z);
}
{% False
}
%#####################################################
\ifthenelse{  \lengthtest{\z pt < 2pt}  }
{
% True
\draw [black]   (\x,\y,\z) -- (\x,\y,\z+1);
}
{% False
}
\shade[rotated axis,ball color = purple!80] (\x,\y,\z) circle (0.06cm);
}

\end{tikzpicture}

\end{document}


The thing with tikz is, since it's so malleable, you can take from several examples and make your own.

Here are a few examples of what you might be looking for (before stretching):

One cuboid : http://www.texample.net/tikz/examples/cuboid/

A commutative diagram : http://www.texample.net/tikz/examples/commutative-diagram/

For what is left, it's a matter of tweaking a base code

A simple 3D graph : http://www.texample.net/tikz/examples/3d-graph-model/

and many other examples : http://www.texample.net/tikz/examples/all/

Click on download as [TEX] if you want to see the inside

I think the stretching part is a matter of point placement

• I tried representing a very general inhomogenous tranformation (anything basically) but this didn't work. Any further guidance? – Sid Aug 26 '15 at 12:26
• as a last resort, I would place the points manually and then the lines would follow, which I am guessing is far from your ideal solution – Amandine FAURILLOU Aug 26 '15 at 13:02