Example: Annotated 3D box

Published 2009-08-27 | Author: Alain Matthes

In the current version of PGF there is a small, undocumented library for 3D drawings. This example shows how you can use it to draw simple, but effective 3D-illustrations.

Download as: [PDF] [TEX]  •  [Open in writeLaTeX]

Annotated 3D box

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de.

% Author   : Alain Matthes
% Encoding : UTF8
% Engine   : PDFLaTeX
% from http://www.fauskes.net/media/code/blend2sketch/img/boxann.png
\documentclass[]{article}
\usepackage[utf8]{inputenc} 
\usepackage{fullpage}
\usepackage{fourier}
\usepackage{tikz}
\usetikzlibrary{calc,3d}

\begin{document}
\thispagestyle{empty} 

\begin{center}
\begin{tikzpicture}[x  = {(-0.5cm,-0.5cm)},
                    y  = {(0.9659cm,-0.25882cm)},
                    z  = {(0cm,1cm)},
                    scale = 2,
                    color = {lightgray}]
% style of faces
\tikzset{facestyle/.style={fill=lightgray,draw=red,very thin,line join=round}}
% face "back" 
\begin{scope}[canvas is zy plane at x=0]
  \path[facestyle,shade] (0,0) rectangle (2,4);
\end{scope}
% face  "left"
\begin{scope}[canvas is zx plane at y=0]
  \path[facestyle,shade] (0,0) rectangle (2,3);
\end{scope}
% face "front"
\begin{scope}[canvas is zy plane at x=3]
  \path[facestyle] (0,0) rectangle (2,4);
\end{scope}
% face  "right"
\begin{scope}[canvas is zx plane at y=4]
  \path[facestyle] (0,0) rectangle (2,3);
\end{scope}
% face "up" 
\draw[fill=brown,draw=red,opacity=.8,very thin,line join=round]
 (0,0,2) -- 
 (3,0,2) --
 (3,{4*cos(15)},{4*sin(15)+2}) --
 (0,{4*cos(15)},{4*sin(15)+2}) --cycle ;
% labels
\draw[very thin,black,line join=round]
     (3,0,0) -- node [sloped,below] {$a$}    
     (3,4,0) -- node [right]        {$b $}
     (3,4,2) -- node [sloped,above] {$c=\sqrt{a^2+b^2}$} 
     (3,0,0);
\end{tikzpicture}
\end{center}
\end{document}

Comments

  • #1 Marian, August 28, 2009 at 3:53 p.m.

    Nice. However, the magnification 3200% of your PDF in the viewer shows that the edges are plotted not correct.

    How can be this problem solved? Thanks.

  • #2 Kjell Magne Fauske, August 29, 2009 at 1:06 p.m.

    @Marian. To reduce the artifacts you can try experimenting with the "line join" graphical option. Setting "line join=round" should give better results. Keep in mind that the drawing is created by drawing each side of the box separately in 2D. They are then transformed to achieve a 3D-effect. This is probably why there are some corner-artifacts

  • #3 Alain Matthes, August 31, 2009 at 3:02 p.m.

    @ Marian

    I agree with you, completely, I make a try with a bad "style" !

    @ Kjell yes with "line join=round", the result is correct but without "double" the edges are plotted more correctly.

    Better is : \tikzset{facestyle/.style={fill=lightgray,draw=red,very thin,line join=round}} etc ...

    I upload the result and the code on my site :

    http://altermundus.com/pages/examples.html

    Best Regards

    Alain Matthes

  • #4 Kjell Magne Fauske, August 31, 2009 at 7:28 p.m.

    I have now updated the example with the new styles.

  • #5 kamrul, August 31, 2010 at 8:09 a.m.

    Hi,

    Can anyone tell me where to find the documentation for the tikzlibrary 3D if there is one?

    Kind regards, Kamrul

  • #6 Edmund, October 22, 2010 at 3:33 a.m.

    I have also been trying to find documentation for the tikz library 3D. Does anybody know where it can be found?

  • #7 Amber Lien, January 29, 2013 at 5:58 p.m.

    Glad I found your post. Very helpful indeed.

Adding comments is currently not enabled.