如何旋转 3D 轴?轴的某些选项不起作用

如何旋转 3D 轴?轴的某些选项不起作用

我有一个修改了方向轴的图表。我想使用view选项axis来旋转图表,因为我想更好地显示 OAP 平面。但是它不起作用。即使我使用axis equalscale mode=scale uniformly选项,三个轴的比例也会发生变化。

即便如此,我还是想理解x=...y=...z=...的工作原理。我知道单位向量,但我不明白这段代码。手册中没有太多关于它如何工作的解释。请有人帮我解决代码和解释。

这是我想要修改的图表的代码。

\documentclass{report}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepackage{siunitx}
\sisetup{per-mode = symbol}
\begin{document}
 \begin{figure}[htbp!]
     \centering
\begin{tikzpicture}
 \begin{axis} [axis lines=center, axis equal,
            x={(.5cm,-0.1cm)}, y={(0cm,0.5cm)}, z={(-0.3cm,-0.3cm)},
            scale mode=scale uniformly,
            xlabel={$x$},
            ylabel={$y$},
            zlabel={$z$},%axis equal=true,
            xmax=6, ymax=4, zmax=7,
            ticks=none,
            clip=false, scale=2, view={0}{90},
            ]
            \addplot3+ [no marks,blue] coordinates { (0,0,0) (4,0,0) (4,0,4) (0,0,4) (0,0,0)};
            \addplot3+ [no marks,blue] coordinates { (0,2,0) (4,2,0) (4,2,4) (0,2,4) (0,2,0)};
            \addplot3+ [no marks,blue,solid] coordinates { (0,0,0) (0,2,0)};
            \addplot3+ [no marks,blue,solid] coordinates { (4,0,0) (4,2,0)};
            \addplot3+ [no marks,blue,solid] coordinates { (4,0,4) (4,2,4)};
            \addplot3+ [no marks,blue,solid] coordinates { (0,0,4) (0,2,4)};
            \node [only marks] at (0,2,4)[above left] {$P(0,2,4)$};
            \filldraw [gray] (0,2,4) circle [radius=2pt];
            \filldraw [fill=gray!10, draw=green!10!black] (0,0,0) -- (0,2,4) -- (4,2,0) -- (0,0,0);
            \node [only marks] at (0,0,0)[below] {$O$};
            \node [only marks] at (4,2,0)[above] {$A(4,2,0)$};
            \draw [very thick] (0,0,0) -- (4,2,0) node [midway, sloped, below] {$y=0.5x$};
            \draw[|{latex}-{latex}|] (4.2,0,4)--(4.2,0,0)
    node [midway,sloped, below] { \SI{4}{mm}};
    \draw[|{latex}-{latex}|] (0,-.2,4)--(4,-.2,4)
    node [midway,sloped, below] { \SI{4}{mm}};
    \draw[|{latex}-{latex}|] (4.2,0,0)--(4.2,2,0)
    node [midway,sloped, below] { \SI{2}{mm}};
        \end{axis}
\end{tikzpicture}
\caption{Paralelepípedo}
\label{planoP}
\end{figure}
\end{document}

在此处输入图片描述

答案1

在我看来,这是 tikz 绘图,比 pgfplots 好。话虽如此,首先要做的事情。因此,对于 的含义x=..., y=..., z=...。当我们为 x、y 和 z 分配一些值时,我们在 2D 平面中定义三个单位向量,它们是 3D 绘图的参考。如果我们写x={(0.5cm,-0.1cm)},y={(0cm,0.5cm)},z={(-0.3cm,-0.3cm)},那么我们将这些向量设置为 x、y 和 z 轴的单位向量,物理学家称之为\vec i, \vec j, \vec k\mathbf{i},\mathbf{j},\mathbf{k}(无论什么)。如果我们想要表示点 B(1,2,0),我们需要从原点移动一次量x=(0.5,-0.1),然后移动两倍量y=(0,0.5),2D 平面上的点 B 将位于

(0.5,-0.1) + 2(0,0.5) = (0.5,0.9)

在此处输入图片描述

但是透视图存在问题。在本例中这不是问题,但对于稍微复杂一点的绘图来说可能是一个问题。假设我们需要在 3D 中绘制一个半径为 2 的球体,并且我们按上述方式定义轴:x={(0.5cm,-0.1cm)},y={(0cm,0.5cm)},z={(-0.3cm,-0.3cm)}。然后手工近似,我们可以得到以下图片,其中红色曲线是球体和坐标平面(半径为 2)的交点圆,蓝色曲线是(不太好的)近似球体。 在此处输入图片描述

这里的问题是,对于轴测透视图,每个轴都需要一个适当的缩小率,我们必须计算这些比率。有很多方法可以做到这一点。例如,有在线计算器(我使用http://tamivox.org/redbear/axono/index.html)、有tikz-3dplot图书馆等等。

如果我们用减速比计算适当的轴,我们可以得到以下代码:

\documentclass[border=2mm]{standalone}
\usepackage    {tikz}
\usetikzlibrary{3d}

\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,
%x={(0.5cm,-0.1cm)},y={(0cm,0.5cm)},z={(-0.3cm,-0.3cm)}                 % original axes
x={(0.9150cm,-0.1779cm)},y={(0cm,0.8976cm)},z={(-0.4034cm,-0.4034cm)}  % something approximate but with corrected reduction
%x={(0cm,0.8976cm)},y={(-0.9150cm,0.1779cm)},z={(-0.4034cm,-0.4034cm)}  % rotated axes
]
% Grids
\draw[canvas is xy plane at z=0,help lines] (0,0) grid (3,3);
\draw[canvas is xz plane at y=0,help lines] (0,0) grid (3,3);
\draw[canvas is yz plane at x=0,help lines] (0,0) grid (3,3);
% Axes
\draw [-latex] (0,0,0) -- (4,0,0) node [right] {$x$};
\draw [-latex] (0,0,0) -- (0,4,0) node [above] {$y$};
\draw [-latex] (0,0,0) -- (0,0,4) node [left]  {$z$};
% Circles
\draw[canvas is xy plane at z=0,red] (0,0) circle (2);
\draw[canvas is xz plane at y=0,red] (0,0) circle (2);
\draw[canvas is yz plane at x=0,red] (0,0) circle (2);
% Sphere
%\draw[blue] (0,0) circle (1.2 cm); % I'm trying to approximate the radius by hand
\draw[blue] (0,0) circle (2 cm);
% B point
%\draw[red,->] (0,0,0) -- (1,0,0) node [midway, below] {$\vec i$};
%\draw[red,->] (1,0,0) -- (1,2,0) node [midway, right] {$2\vec j$};
%\fill (1,2,0) circle (1pt) node [above right] {$B$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

现在开始绘图。如果我理解你需要旋转(绕 z 轴),我们只需要将负旧 x 向量的坐标分配给 y 向量,并将旧 y 向量分配给 x 向量(在前面的代码中注释了旋转的轴)。然后,如果我们(使用 tikz)绘制正交曲面,我们可以得到如下结果:

\documentclass[border=2mm]{standalone}
\usepackage    {siunitx}  % to manage SI units
\usepackage    {tikz}
\usetikzlibrary{3d}       % for the "canvas is..." options
\usetikzlibrary{babel}    % not necessary in this example, but just in case...

\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,x={(0cm,0.8976cm)},y={(-0.9150cm,0.1779cm)},z={(-0.4034cm,-0.4034cm)}]
% Axes
\draw [-latex] (0,0,0) -- (5,0,0) node [right] {$x$};
\draw [-latex] (0,0,0) -- (0,5,0) node [above] {$y$};
\draw [-latex] (0,0,0) -- (0,0,5) node [below]  {$z$};
% Hidden lines
\draw[blue,dashed] (0,2,4) -- (0,2,0) -- (4,2,0);
% OAP plane
\draw[green!50!black,fill=green,fill opacity=0.2] (0,0,0) -- (4,2,0) -- (0,2,4) -- cycle;
% Rectangles (faces)
\draw[canvas is xy plane at z=4,blue,fill=blue,fill opacity=0.1] (0,0) rectangle (4,2);
\draw[canvas is yz plane at x=4,blue,fill=blue,fill opacity=0.1] (0,0) rectangle (2,4);
\draw[canvas is xz plane at y=0,blue,fill=blue,fill opacity=0.1] (0,0) rectangle (4,4);
% Labels
\fill (0,0,0) circle (1pt) node [above right] {$O$};
\fill (4,2,0) circle (1pt) node [above]       {$A(4,2,0)$};
\fill (0,2,4) circle (1pt) node [left]        {$P(0,2,4)$};
% Dimensions
\begin{scope}[canvas is xy plane at z=0,red]
  \draw (0,-0.1) -- (0,-0.5);
  \draw (4,-0.1) -- (4,-0.5);
  \draw[<->] (0,-0.4) -- (4,-0.4) node[midway,rotate=90,yshift={-2mm}] {\SI{4}{mm}};
\end{scope}
\begin{scope}[canvas is yz plane at x=0,red]
  \draw (-0.1,4) -- (-0.5,4);
  \draw[<->] (-0.4,0) -- (-0.4,4) node[midway,rotate=45,yshift={-2mm}] {\SI{4}{mm}};
  \draw (2,4.1) -- (2,4.5);
  \draw[<->] (0,4.4) -- (2,4.4) node[midway,rotate=-10,yshift={-2mm}] {\SI{2}{mm}};;
\end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

您可以3dtools使用这里并尝试此代码。

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc,3dtools}% https://github.com/marmotghost/tikz-3dtools
\begin{document}
            \begin{tikzpicture}[line cap=round,line join=round,c/.style={circle,fill,inner sep=1pt}, 3d/install view={phi=30,psi=0,theta=70}]
            \path
             (0,0,0) coordinate (O)
            (4,0,0) coordinate (B)
            (4,2,0) coordinate (A)
            (0,2,0) coordinate (D)
            (0,0,4) coordinate (O')
            (4,0,4) coordinate (B')
            (4,2,4) coordinate (A')
            (0,2,4) coordinate (P);
\path foreach \p/\g in {A/-90,B/-90,O/-90,D/-90,O'/180,B'/90,A'/90,P/90}{(\p)node[c]{}+(\g:2.5mm) node{$\p$}}; 
\draw[3d/visible] (O') -- (O) -- (B) -- (A) 
(B') -- (A') -- (P) -- (O') -- cycle
(B) -- (B') (A) -- (A')  ;
\draw[3d/hidden] (O) -- (D) -- (A)
(D) -- (P) (O) -- (A) -- (P) -- cycle
;
\draw[-latex,3d/visible] (O') -- (0,0,5.5) node[anchor=south]{$z$};
\draw[-latex,3d/visible] (B) -- (6,0,0) node[anchor=south]{$x$};
\draw[-latex,3d/hidden] (D) -- (0,4,0) node[anchor=south]{$y$};
\end{tikzpicture}
\end{document}  

在此处输入图片描述

或者你可以使用

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc,3dtools}% https://github.com/marmotghost/tikz-3dtools
\begin{document}
\foreach \Angle in {5,15,...,355}
{
    \begin{tikzpicture}[line cap=round,line join=round,c/.style={circle,fill,inner sep=1pt}, 3d/install view={phi=\Angle,psi=0,theta=70},same bounding box=A]
            \path
             (0,0,0) coordinate (O)
            (4,0,0) coordinate (B)
            (4,2,0) coordinate (A)
            (0,2,0) coordinate (D)
            (0,0,4) coordinate (O')
            (4,0,4) coordinate (B')
            (4,2,4) coordinate (A')
            (0,2,4) coordinate (P)
            (2,1,2) coordinate (T)
            ;   \tikzset{3d/polyhedron/.cd,O={(T)},fore/.append style={fill=none},
                back/.append style={3d/hidden},
                draw face with corners={{(O)},{(B)},{(A)},{(D)}},
                draw face with corners={{(A)},{(B)},{(B')},{(A')}},
                draw face with corners={{(O)},{(D)},{(P)}},draw face with corners={{(O)},{(O')},{(P)}},
                draw face with corners={{(A')},{(B')},{(O')},{(P)}},
                draw face with corners={{(A)},{(D)},{(P)}},
                draw face with corners={{(A)},{(A')},{(P)}},
                draw face with corners={{(O)},{(B)},{(B')},{(O')}}}
\path foreach \p/\g in {A/-90,O/-90,P/90}{(\p)node[c]{}+(\g:2.5mm) node{$\p$}}; 
\draw[3d/hidden]  (O) -- (A) ;
\end{tikzpicture}}
\end{document}  

在此处输入图片描述

这是来自 marmot 的代码

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc,3dtools}% https://github.com/marmotghost/tikz-3dtools
\begin{document}
    \foreach \Angle in {5,15,...,355}
    {
        \begin{tikzpicture}[line cap=round,line join=round,
            c/.style={circle,fill,inner sep=1pt}, 
            3d/install view={phi=\Angle,theta=70},same bounding box=A]
            \path (1,0,0) coordinate (ez) (0,1,0) coordinate (ex)
            (0,0,1) coordinate (ey);
            \begin{scope}[x={(ex)},y={(ey)},z={(ez)}] 
                \draw[-stealth] (0,0,0) -- (5,0,0) node[pos=1.05]{$x$};
                \draw[-stealth] (0,0,0) -- (0,5,0) node[pos=1.05]{$y$};
                \draw[-stealth] (0,0,0) -- (0,0,5) node[pos=1.05]{$z$};
                \path
                (0,0,0) coordinate (O)
                (4,0,0) coordinate (B)
                (4,2,0) coordinate (A)
                (0,2,0) coordinate (D)
                (0,0,4) coordinate (O')
                (4,0,4) coordinate (B')
                (4,2,4) coordinate (A')
                (0,2,4) coordinate (P)
                (2,1,2) coordinate (T);   
                \tikzset{3d/polyhedron/.cd,O={(T)},fore/.append style={fill=none},
                    back/.append style={3d/hidden},
                    draw face with corners={{(O)},{(B)},{(A)},{(D)}},
                    draw face with corners={{(A)},{(B)},{(B')},{(A')}},
                    draw face with corners={{(O)},{(D)},{(P)}},draw face with corners={{(O)},{(O')},{(P)}},
                    draw face with corners={{(A')},{(B')},{(O')},{(P)}},
                    draw face with corners={{(A)},{(D)},{(P)}},
                    draw face with corners={{(A)},{(A')},{(P)}},
                    draw face with corners={{(O)},{(B)},{(B')},{(O')}}}
                \path foreach \p/\g in {A/-90,O/-90,P/90}{(\p)node[c]{}+(\g:2.5mm) node{$\p$}}; 
                \draw[3d/hidden]  (O) -- (A) ;
            \end{scope}
    \end{tikzpicture}}
\end{document} 

在此处输入图片描述

相关内容