7

I need to draw a 3d isometric grid/lattice using tikz that is not visually confusing. It needs to be at least 6x6x6 but possibly bigger. I sort of would like to have it fade into the background or contracts in size as it goes into the screen. I will put small text nodes at each coordinate. For any example dots will suffice.

It is analogous to http://www.texample.net/tikz/examples/lattice-points/ except in 3D. The main issue is somehow figuring out how to make sure it is easily seen in 3D.

4
  • Does realistic refer to the perspective in this context?
    – percusse
    Commented Aug 31, 2012 at 16:40
  • @percusse Not so much. By realistic I mean it shouldn't look like 2D, the viewer shouldn't get easily confused by which line goes with which axis, and objects at some lattice point should easily be a seen at that point and not possibly some other point. Similar: projects.skewed.de/graph-tool/doc/_images/lattice_3d.png but visually a mess. (I understand it is difficult or near impossible to make it ideal but maybe someone has some tricks to get decent results) Commented Aug 31, 2012 at 16:53
  • My lattice is about 6x6x6 but I won't label every lattice point. Just the foreground ones a layer or two deep(I need to label enough to show the pattern that exists on the lattice). Commented Aug 31, 2012 at 16:55
  • Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. Commented Jan 10, 2019 at 19:54

3 Answers 3

10

Something like this? And you don't want isometric (angles 30/150/90), trust me ;)

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{3d}

\begin{document}

\newcommand{\xangle}{15}
\newcommand{\yangle}{153}
\newcommand{\zangle}{90}

\newcommand{\xlength}{1}
\newcommand{\ylength}{1}
\newcommand{\zlength}{1}

\newcommand{\dimension}{5}% actually dimension-1

\pgfmathsetmacro{\xx}{\xlength*cos(\xangle)}
\pgfmathsetmacro{\xy}{\xlength*sin(\xangle)}
\pgfmathsetmacro{\yx}{\ylength*cos(\yangle)}
\pgfmathsetmacro{\yy}{\ylength*sin(\yangle)}
\pgfmathsetmacro{\zx}{\zlength*cos(\zangle)}
\pgfmathsetmacro{\zy}{\zlength*sin(\zangle)}

\begin{tikzpicture}
[   x={(\xx cm,\xy cm)},
    y={(\yx cm,\yy cm)},
    z={(\zx cm,\zy cm)},
]
\foreach \a in {0,...,\dimension}
{   \foreach \b in {0,...,\dimension}
    {   \pgfmathsetmacro{\c}{100-\a*7-\b*7}
        \draw[canvas is xy plane at z=\a, black!\c] (\b,0) -- (\b,\dimension) (0,\b) -- (\dimension,\b);
        \draw[canvas is xz plane at y=\a, black!\c] (\b,0) -- (\b,\dimension) (0,\b) -- (\dimension,\b);
        \draw[canvas is yz plane at x=\a, black!\c] (\b,0) -- (\b,\dimension) (0,\b) -- (\dimension,\b);
    }
}

\foreach \a in {0,...,\dimension}
{   \foreach \b in {0,...,\dimension}
    {   \foreach \c in {0,...,\dimension}
        {   \fill (\a,\b,\c) circle (0.05cm);
        }
    }
}   
\end{tikzpicture}

\end{document}

Result

enter image description here


Edit 1: Some improvements: The fading computation is better, and the cuboid is constructed from back to front (if zangle≈270, yangle≈150, xangle≈30). Does it have to be a cube, or is a cuboid sufficient?

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{3d}
\usepackage{xifthen}

\begin{document}

\newcommand{\xangle}{11}
\newcommand{\yangle}{133}
\newcommand{\zangle}{270}

\newcommand{\xlength}{1}
\newcommand{\ylength}{1}
\newcommand{\zlength}{1}

% nice result for 30 150 270 1 1.414 1.732
% nice result for 11 133 270 1 1 1

\newcommand{\dimension}{6}% actually dimension-1

\pgfmathsetmacro{\xx}{\xlength*cos(\xangle)}
\pgfmathsetmacro{\xy}{\xlength*sin(\xangle)}
\pgfmathsetmacro{\yx}{\ylength*cos(\yangle)}
\pgfmathsetmacro{\yy}{\ylength*sin(\yangle)}
\pgfmathsetmacro{\zx}{\zlength*cos(\zangle)}
\pgfmathsetmacro{\zy}{\zlength*sin(\zangle)}

\begin{tikzpicture}
[   x={(\xx cm,\xy cm)},
    y={(\yx cm,\yy cm)},
    z={(\zx cm,\zy cm)},
]

\foreach \x in {\dimension,...,0}
{   \foreach \y in {\dimension,...,0}
    {   \foreach \z in {\dimension,...,0}
        {   \pgfmathsetmacro{\c}{100-(\x*\y*\z)/(\dimension*\dimension*\dimension)*95}
            \ifthenelse{\x>0}
                {\draw[black!\c] (\x,\y,\z) -- (\x-1,\y,\z);}{}
            \ifthenelse{\y>0}
                {\draw[black!\c] (\x,\y,\z) -- (\x,\y-1,\z);}{}
            \ifthenelse{\z>0}
                {\draw[black!\c] (\x,\y,\z) -- (\x,\y,\z-1);}{}     
            \fill[red!\c] (\x,\y,\z) circle (0.05cm);   
        }
    }
}

\foreach \x/\y/\z/\lab in {0/0/4/Bla,1/5/0/Bli,1/1/1/Blubb} 
{   \fill[blue] (\x,\y,\z) circle (0.05cm) node[fill=white,rounded corners=2mm,fill opacity=0.5,text opacity=1,above right,inner sep=2pt] {\lab};
}   

\end{tikzpicture}

\end{document}

Output

enter image description here

Output cuboid

\newcommand{\xangle}{30}
\newcommand{\yangle}{150}
\newcommand{\zangle}{270}

\newcommand{\xlength}{1}
\newcommand{\ylength}{1.414}
\newcommand{\zlength}{1.732}

enter image description here

1
  • 3
    "We are the Borg. We will add your biological and TeXnological distinctiveness to our own. Resistance is futile." Commented Sep 3, 2012 at 7:07
6

run with latex->dvips->ps2pdf

\documentclass{article}
\usepackage{pst-gr3d}\SpecialCoor
\begin{document}

\psset{unit=1.3cm}
\PstGridThreeD[GridThreeDNodes](1,2,2)
\psset{arrows=<->,arrowscale=2}
\ThreeDput[normal=0 0 -1](0,0,0){%
  \ncloop[linecolor=red,arm=0.35,loopsize=0.6,
          angleA=-90,angleB=90]{Gr3dNode022}{Gr3dNode002}
  \ncloop[linecolor=green,arm=0.7,nodesepA=0.18,nodesepB=0.12,
           loopsize=-0.5,angleA=180]{Gr3dNode002}{Gr3dNode102}}
\qquad%
\PstGridThreeD[GridThreeDNodes](4,3,3)
\nccurve[ncurv=2,linecolor=red]{->}{Gr3dNode000}{Gr3dNode433}

\end{document}

enter image description here

3
  • The three thick balck lines are just PDF-viewer artifacts, I suppose? Commented Sep 3, 2012 at 6:39
  • yes, that is only a problem with my pdf viewer
    – user2478
    Commented Sep 3, 2012 at 6:46
  • I know the problem. Usually sends me panicing and staring at my code thinking "where did something go wrong?" until I realizr that :D Commented Sep 3, 2012 at 7:04
2

Here is a proposal, to a large extent stolen from Tom Bombadil's answer, which has some sort of fading.

\documentclass[tikz]{standalone}
\usetikzlibrary{3d}

\begin{document}

\newcommand{\xangle}{15}
\newcommand{\yangle}{153}
\newcommand{\zangle}{90}

\newcommand{\xlength}{1}
\newcommand{\ylength}{1}
\newcommand{\zlength}{1}

\newcommand{\dimension}{5}% actually dimension-1

\pgfmathsetmacro{\xx}{\xlength*cos(\xangle)}
\pgfmathsetmacro{\xy}{\xlength*sin(\xangle)}
\pgfmathsetmacro{\yx}{\ylength*cos(\yangle)}
\pgfmathsetmacro{\yy}{\ylength*sin(\yangle)}
\pgfmathsetmacro{\zx}{\zlength*cos(\zangle)}
\pgfmathsetmacro{\zy}{\zlength*sin(\zangle)}

\begin{tikzpicture}
[   x={(\xx cm,\xy cm)},
    y={(\yx cm,\yy cm)},
    z={(\zx cm,\zy cm)},
]
\pgfmathtruncatemacro{\dimmax}{\dimension+3}
\foreach \a in {0,...,\dimmax}
{ \pgfmathtruncatemacro{\dima}{min(\dimension+\a,\dimension+3)}  \foreach \b in {0,...,\dima}
    { \pgfmathtruncatemacro{\dimb}{\dimension+\b+1}
    \ifnum\a<\dimb 
    \pgfmathtruncatemacro{\dimb}{\dimension+\b-\a} \foreach \c in {0,...,\dimension}
        {
        \pgfmathsetmacro{\opa}{max(1-0.12*sqrt(\a^2+\b^2),0)}
        \begin{scope}[opacity=\opa]   
        \fill (\a,\b,\c) circle (0.05cm);
        \draw[canvas is xy plane at z=\c] (\a,\b) -- (\a,\b+1) (\a,\b) -- (\a+1,\b);
        \ifnum\c<\dimension
        \draw[canvas is xz plane at y=\b] (\a,\c) -- (\a,\c+1);
        \fi
        \end{scope}
        }
    \else
    \fi
    }
}   
\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 .