3

I have the following file to generate an animation with a separate file

\documentclass[tikz, border=1cm]{standalone}

\usepackage{tkz-euclide}
\usetkzobj{all}

\begin{document}

\foreach \vari in {0,5,10,15,...,360}
{

  \begin{tikzpicture}
    [
      scale=.5,
      x={(-1cm,-.25cm)},y={(1cm,-.25cm)},z={(0cm,1cm)},
      rotate around z=\vari
    ]

        % coordinates

        \coordinate (A) at (3,-3,0);
        \coordinate (B) at (3,3,0);
        \coordinate (C) at (-3,3,0);
        \coordinate (D) at (-3,-3,0);
        \coordinate (E) at (3,-3,3);
        \coordinate (F) at (3,3,3);
        \coordinate (G) at (-3,3,3);
        \coordinate (H) at (-3,-3,3);

        \tkzDrawPolygon[fill=blue!20,opacity=.5,rounded corners=.1pt](C,D,H,G)

        % Axes 3d

        \draw[->,>=stealth] (3,0,0) -- (5,0,0);
        \draw[->,>=stealth] (0,3,0) -- (0,5,0);
        \draw[dashed] (0,0,0) -- (0,0,4);
        \draw[dashed] (0,0,0) -- (0,3,0);
        \draw[dashed] (0,0,0) -- (3,0,0);

        \tkzDrawPolygon[fill=blue!20,opacity=.5,rounded corners=.1pt](A,B,C,D)
        \tkzDrawPolygon[fill=blue!20,opacity=.5,rounded corners=.1pt](B,C,G,F)
        \tkzDrawPolygon[fill=blue!20,opacity=.5,rounded corners=.1pt](A,D,H,E)
        \tkzDrawPolygon[fill=blue!20,opacity=.5,rounded corners=.1pt](A,B,F,E)
        \tkzDrawPolygon[fill=blue!20,opacity=.5,rounded corners=.1pt](G,H,E,F)

        \tkzDrawSegments[ultra thick](A,B B,C C,G G,H H,E E,F E,A F,B F,G)
        \tkzLabelPoints[above](E,F,G,H)
        \tkzLabelPoints[below](A,B,C,D)

        \draw[->,>=stealth] (0,0,4) -- (0,0,7); 
    \end{tikzpicture}  
}
\end{document}

but my problem is that the point (0,0,0) changes place during rotation and I would like to have it in a single place throughout the entire animation. Is there a way to anchor it?

Thank you.

5

It is not the point (0,0,0) that changes but the bounding box. To prevent this from happening, you can set a bounding box yourself, e.g. by saying

  \path[use as bounding box] (-4,-1.45) rectangle (4,4);

This yields

\documentclass[tikz, border=1cm]{standalone}

\usepackage{tkz-euclide}
\usetkzobj{all}

\begin{document}

\foreach \vari in {0,5,10,15,...,360}
{

  \begin{tikzpicture}
  \path[use as bounding box] (-4,-1.45) rectangle (4,4);
  \begin{scope}
    [
      scale=.5,
      x={(-1cm,-.25cm)},y={(1cm,-.25cm)},z={(0cm,1cm)},
      rotate around z=\vari
    ]

        % coordinates
        \coordinate (A) at (3,-3,0);
        \coordinate (B) at (3,3,0);
        \coordinate (C) at (-3,3,0);
        \coordinate (D) at (-3,-3,0);
        \coordinate (E) at (3,-3,3);
        \coordinate (F) at (3,3,3);
        \coordinate (G) at (-3,3,3);
        \coordinate (H) at (-3,-3,3);

        \tkzDrawPolygon[fill=blue!20,opacity=.5,rounded corners=.1pt](C,D,H,G)

        % Axes 3d

        \draw[->,>=stealth] (3,0,0) -- (5,0,0);
        \draw[->,>=stealth] (0,3,0) -- (0,5,0);
        \draw[dashed] (0,0,0) -- (0,0,4);
        \draw[dashed] (0,0,0) -- (0,3,0);
        \draw[dashed] (0,0,0) -- (3,0,0);

        \tkzDrawPolygon[fill=blue!20,opacity=.5,rounded corners=.1pt](A,B,C,D)
        \tkzDrawPolygon[fill=blue!20,opacity=.5,rounded corners=.1pt](B,C,G,F)
        \tkzDrawPolygon[fill=blue!20,opacity=.5,rounded corners=.1pt](A,D,H,E)
        \tkzDrawPolygon[fill=blue!20,opacity=.5,rounded corners=.1pt](A,B,F,E)
        \tkzDrawPolygon[fill=blue!20,opacity=.5,rounded corners=.1pt](G,H,E,F)

        \tkzDrawSegments[ultra thick](A,B B,C C,G G,H H,E E,F E,A F,B F,G)
        \tkzLabelPoints[above](E,F,G,H)
        \tkzLabelPoints[below](A,B,C,D)

        \draw[->,>=stealth] (0,0,4) -- (0,0,7); 
    \end{scope} 
    \end{tikzpicture}  
}
\end{document}

enter image description here

However, I would recommend tikz-3dplot for that. It allows you to do orthographic projections, and makes it more easy to distinguish hidden from foreground faces. I also use [a routine to determine the minimal bounding box that fits all frames. It also comes with some style for the hidden surfaces, and I added some annotations to the code that may help to understand more easily where to modify it.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\tikzset{plane/.style n args={3}{insert path={%
#1 -- ++ #2 -- ++ #3 -- ++ ($-1*#2$) -- cycle}},
xy plane/.style={plane={#1}{(\pgfkeysvalueof{/tikz/3d block/x length},0,0)}{(0,\pgfkeysvalueof{/tikz/3d block/y length},0)}},
xz plane/.style={plane={#1}{(\pgfkeysvalueof{/tikz/3d block/x length},0,0)}{(0,0,\pgfkeysvalueof{/tikz/3d block/z length})}},
yz plane/.style={plane={#1}{(0,\pgfkeysvalueof{/tikz/3d block/y length},0)}{(0,0,\pgfkeysvalueof{/tikz/3d block/z length})}},
get projections/.style={insert path={%
let \p1=(1,0,0),\p2=(0,1,0)  in 
[/utils/exec={\pgfmathtruncatemacro{\xproj}{sign(\x1)}\xdef\xproj{\xproj}
\pgfmathtruncatemacro{\yproj}{sign(\x2)}\xdef\yproj{\yproj}
\pgfmathtruncatemacro{\zproj}{sign(cos(\tdplotmaintheta))}\xdef\zproj{\zproj}}]}},
pics/3d block/.style={code={
\tikzset{3d block/.cd,#1}
\path[get projections];
% start drawing the "hidden surfaces"
\ifnum\zproj=-1
 \path[3d block/every face,3d block/hidden face,3d block/xy face,xy plane={
 (-\pgfkeysvalueof{/tikz/3d block/x length}/2,
  -\pgfkeysvalueof{/tikz/3d block/y length}/2,
  -\pgfkeysvalueof{/tikz/3d block/z length}/2)}]; 
\else
 \path[3d block/every face,3d block/hidden face,3d block/xy face,xy plane={
 (-\pgfkeysvalueof{/tikz/3d block/x length}/2,
  -\pgfkeysvalueof{/tikz/3d block/y length}/2,
  \pgfkeysvalueof{/tikz/3d block/z length}/2)}]; 
\fi
\ifnum\yproj=1
 \path[3d block/every face,3d block/hidden face,3d block/yz face,yz plane={
 (-\pgfkeysvalueof{/tikz/3d block/x length}/2,
  -\pgfkeysvalueof{/tikz/3d block/y length}/2,
  -\pgfkeysvalueof{/tikz/3d block/z length}/2)}]; 
\else
 \path[3d block/every face,3d block/hidden face,3d block/yz face,yz plane={
 (\pgfkeysvalueof{/tikz/3d block/x length}/2,
  -\pgfkeysvalueof{/tikz/3d block/y length}/2,
  -\pgfkeysvalueof{/tikz/3d block/z length}/2)}]; 
\fi
\ifnum\xproj=1
 \path[3d block/every face,3d block/hidden face,3d block/xz face,xz plane={
 (-\pgfkeysvalueof{/tikz/3d block/x length}/2,
  \pgfkeysvalueof{/tikz/3d block/y length}/2,
  -\pgfkeysvalueof{/tikz/3d block/z length}/2)}]; 
\else
 \path[3d block/every face,3d block/hidden face,3d block/xz face,xz plane={
 (-\pgfkeysvalueof{/tikz/3d block/x length}/2,
  -\pgfkeysvalueof{/tikz/3d block/y length}/2,
  -\pgfkeysvalueof{/tikz/3d block/z length}/2)}]; 
\fi
% end drawing the "hidden surfaces"
% start drawing the "forefronts"
\ifnum\yproj=1
 \path[3d block/every face,3d block/yz face,yz plane={
 (\pgfkeysvalueof{/tikz/3d block/x length}/2,
  -\pgfkeysvalueof{/tikz/3d block/y length}/2,
  -\pgfkeysvalueof{/tikz/3d block/z length}/2)}]; 
\else
 \path[3d block/every face,3d block/yz face,yz plane={
 (-\pgfkeysvalueof{/tikz/3d block/x length}/2,
  -\pgfkeysvalueof{/tikz/3d block/y length}/2,
  -\pgfkeysvalueof{/tikz/3d block/z length}/2)}]; 
\fi
\ifnum\xproj=1
 \path[3d block/every face,3d block/xz face,xz plane={
 (-\pgfkeysvalueof{/tikz/3d block/x length}/2,
  -\pgfkeysvalueof{/tikz/3d block/y length}/2,
  -\pgfkeysvalueof{/tikz/3d block/z length}/2)}]; 
\else
 \path[3d block/every face,3d block/xz face,xz plane={
 (-\pgfkeysvalueof{/tikz/3d block/x length}/2,
  \pgfkeysvalueof{/tikz/3d block/y length}/2,
  -\pgfkeysvalueof{/tikz/3d block/z length}/2)}]; 
\fi
\ifnum\zproj>-1
 \path[3d block/every face,3d block/xy face,xy plane={
 (-\pgfkeysvalueof{/tikz/3d block/x length}/2,
  -\pgfkeysvalueof{/tikz/3d block/y length}/2,
  \pgfkeysvalueof{/tikz/3d block/z length}/2)}]; 
\else
 \path[3d block/every face,3d block/xy face,xy plane={
 (-\pgfkeysvalueof{/tikz/3d block/x length}/2,
  -\pgfkeysvalueof{/tikz/3d block/y length}/2,
  -\pgfkeysvalueof{/tikz/3d block/z length}/2)}]; 
\fi
}},
3d block/.cd,
xy face/.style={fill=blue!20,opacity=.5,rounded corners=.1pt},
xz face/.style={fill=blue!20,opacity=.5,rounded corners=.1pt},
yz face/.style={fill=blue!20,opacity=.5,rounded corners=.1pt},
every face/.style={draw,very thick},
hidden face/.style={dashed,thick},
x length/.initial=1,
y length/.initial=1,
z length/.initial=1,
/tikz/retrieve bounding box/.style={insert path={
    let \p1=(current bounding box.south west),
    \p2=(current bounding box.north east)
    in \pgfextra{%
    \pgfmathsetmacro{\xmin}{min(\x1,\xmin)}
    \pgfmathsetmacro{\xmax}{max(\x2,\xmax)}
    \pgfmathsetmacro{\ymin}{min(\y1,\ymin)}
    \pgfmathsetmacro{\ymax}{max(\y2,\ymax)}
    \xdef\xmin{\xmin pt}
    \xdef\xmax{\xmax pt}    
    \xdef\ymin{\ymin pt}
    \xdef\ymax{\ymax pt}  
    %\typeout{\xmin,\xmax,\ymin,\ymax}  
    }}}}
\pgfmathsetmacro{\xmin}{0}
\pgfmathsetmacro{\xmax}{0}
\pgfmathsetmacro{\ymin}{0}
\pgfmathsetmacro{\ymax}{0}
\begin{document}
\foreach \vari in {0,5,10,15,...,360}
{\tdplotsetmaincoords{70}{-\vari} % the first argument cannot be larger than 90
\begin{tikzpicture}[line join=round,tdplot_main_coords,>=stealth]
 \ifdefined\figbb\relax
    \path \figbb;
    \fi
 \coordinate (A) at (3,-3,-1.0);
 \coordinate (B) at (3,3,-1.0);
 \coordinate (C) at (-3,3,-1.0);
 \coordinate (D) at (-3,-3,-1.0);
 \coordinate (E) at (3,-3,1.0);
 \coordinate (F) at (3,3,1.0);
 \coordinate (G) at (-3,3,1.0);
 \coordinate (H) at (-3,-3,1.0);    
 \foreach \X in {A,B,C,D}
 {\path (\X) node[below]{\X};}
 \draw[dashed] (3,0,-1.0) -- (0,0,-1.0) -- (0,3,-1.0)
 (0,0,-1.0) -- (0,0,1.0);
 \draw[thick,->] (3,0,-1.0) -- (5,0,-1.0);
 \draw[thick,->] (0,3,-1.0) -- (0,5,-1.0);
 \pic{3d block={x length=6,y length=6,z length=2}};
 \draw[thick,->] (0,0,1.0) -- (0,0,4);
 \foreach \X in {E,F,G,H}
 {\path (\X) node[above]{\X};}
 \path[retrieve bounding box];
\end{tikzpicture}}
\makeatletter               
\edef\figbb{(\xmin,\ymin) rectangle (\xmax,\ymax)}
\immediate\write\@mainaux{\xdef\string\figbb{\figbb}\relax}
\makeatother
\end{document}

enter image description here

  • Do you mean load tikz-3dplot instead of tikz package? Is there a possible significant reduction in code complexity? I haven't looked into tikz-3dplot package so I don't know the commands available in it. – Concept7 Jul 7 at 18:59
  • 2
    @Concept7 The tikz-3dplot package loads tikz and allows you to set the view angles and then does an orthographic projection. That is, it will replace ` x={(-1cm,-.25cm)},y={(1cm,-.25cm)},z={(0cm,1cm)},` by something entirely fixed by the view angles. – user121799 Jul 7 at 19:03
  • 2
    @Concept7 I added a tikz-3dplot version. True, one needs more preparations, but you can adjust the view more easily and can also more easily distinguish between foreground and background faces. – user121799 Jul 7 at 19:45
  • Thanks! I have to study your example but all I can say right now is that it looks impressive!! – Concept7 Jul 8 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.