如何将原点 (0,0,0) 锚定到 tikz 中生成的多个图像的中心?

如何将原点 (0,0,0) 锚定到 tikz 中生成的多个图像的中心?

我有以下文件来用单独的文件生成动画

\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}

但我的问题是点 (0,0,0) 在旋转过程中会改变位置,而我希望在整个动画过程中它都位于一个位置。有没有办法固定它?

谢谢。

答案1

改变的不是点,(0,0,0)而是边界框。为了防止这种情况发生,你可以自己设置边界框,例如

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

这得出

\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}

在此处输入图片描述

但是,我建议tikz-3dplot这样做。它允许您进行正交投影,并且更容易区分隐藏面和前景面。我还使用 [一个例程来确定适合所有帧的最小边界框。它还带有一些隐藏表面的样式,我在代码中添加了一些注释,可能有助于更轻松地理解在哪里修改它。

\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}

在此处输入图片描述

相关内容