2D 矩形的 3D 视图

2D 矩形的 3D 视图

我将非常感激能够知道如何:

  1. 修复代码,让其正常工作
  2. 能够控制视角,这样我就能看到像这幅图顶面那样的矩形

在此处输入图片描述

\documentclass{article}
\usepackage{tikz-3dplot}
\usetikzlibrary{perspective,calc,3d,arrows}
\begin{document}
    
    \tdplotsetmaincoords{70}{110} % Set the view angles
    
    \begin{tikzpicture}[scale=2, transform shape, tdplot_main_coords]
        % Define the coordinates of the rectangle corners with labels
        \coordinate (A) at (0,0,0);
        \coordinate (B) at (4,0,0);
        \coordinate (C) at (4,1,0);
        \coordinate (D) at (0,1,0);
        
        % Draw the rectangle
        \draw (A) -- (B) -- (C) -- (D) -- cycle;
        
        % Draw x-axis
        \draw[->] (0,0,0) -- (2,0,0) node[anchor=north east]{$x$};
        
        % Draw y-axis
        \draw[->] (0,0,0) -- (0,2,0) node[anchor=north west]{$y$};
        
        % Draw z-axis
        \draw[->] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};
        
        % Add labels to coordinates
        \node[above right] at (A) {$A$};
        \node[above right] at (B) {$B$};
        \node[above right] at (C) {$C$};
        \node[above right] at (D) {$D$};
    \end{tikzpicture}
    
\end{document}

答案1

像这样:

\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\usetikzlibrary{perspective,calc,3d,arrows}
\begin{document}
    
    \tdplotsetmaincoords{75}{110} % Set the view angles
    
    \begin{tikzpicture}[scale=2, transform shape, tdplot_main_coords]
        % Define the coordinates of the rectangle corners with labels
        \coordinate (A) at (0,0,1);
        \coordinate (B) at (4,0,1);
        \coordinate (C) at (4,1,1);
        \coordinate (D) at (0,1,1);
        \coordinate (E) at (4,0,0);
        \coordinate (F) at (4,1,0);
        \coordinate (G) at (0,1,0);
        \coordinate (H) at (0,0,0);
        
        % Draw the rectangle
        \draw (A) -- (B) -- (C) -- (D) -- cycle;

        \draw (B) -- (E) -- (F) -- (G) -- (D);

        \draw (C) -- (F);
        
        % Draw x-axis
        \draw[dashed] (0,0,0) -- (4,0,0);
        \draw[->] (4,0,0) -- (6,0,0) node[anchor=north east]{$x$};
        
        % Draw y-axis
        \draw [dashed] (0,0,0) -- (0,1,0);
        \draw [->] (0,1,0) --  (0,2,0) node[anchor=north west]{$y$};
        
        % Draw z-axis
        \draw[dashed] (0,0,0) -- (0,0,1);
        \draw [->] (0,0,1) --  (0,0,2)
        node[anchor=south]{$z$};
        
        % Add labels to coordinates
        \node[above right] at (A) {$A$};
        \node[above left] at (B) {$B$};
        \node[above] at (C) {$C$};
        \node[above right] at (D) {$D$};
        \node[below] at (E) {$E$};
        \node[below right] at (F) {$F$};
        \node[below] at (G) {$G$};
        \node[below] at (H) {$H$};
    \end{tikzpicture}
    
\end{document}

在此处输入图片描述

答案2

像这样:

在此处输入图片描述

代码:

\documentclass{article}
\usepackage{tikz,tikz-3dplot}
\usetikzlibrary{perspective,calc,3d,arrows}
\begin{document}
    
    \tdplotsetmaincoords{80}{110} % Set to {0}{90} to view only red face
    
    \begin{tikzpicture}[scale=2, transform shape, tdplot_main_coords]
        % Define the coordinates of the rectangle corners with labels
        \coordinate (A) at (0,0,0);
        \coordinate (B) at (4,0,0);
        \coordinate (C) at (4,1,0);
        \coordinate (D) at (0,1,0);
        \coordinate (A1) at (0,0,1);
        \coordinate (B1) at (4,0,1);
        \coordinate (C1) at (4,1,1);
        \coordinate (D1) at (0,1,1);
        
            % Draw x-axis
        \draw[->] (B) -- (5,0,0) node[left]{$x$};
        
            % Draw y-axis
        \draw[,->] (D) -- (0,2,0) node[above]{$y$};
                
            % Draw z-axis
        \draw[->] (A1) -- (0,0,2) node[above]{$z$};
        
            % Draw the rectangle
        \fill[violet] (C) -- (C1) -- (D1) -- (D) -- cycle;
        \fill[cyan] (B) -- (B1) -- (C1) -- (C) -- cycle;
        \fill[red] (A1) -- (B1) -- (C1) -- (D1) -- cycle;
        
        \draw[dashed,line width=1pt] (A)--(A1);
        \draw[dashed,line width=1pt] (A)--(B);
        \draw[dashed,line width=1pt] (A)--(D);
        
        % Add labels to coordinates
%       \node[above right] at (A) {$A$};
%       \node[below] at (B) {$B$};
%       \node[below] at (C) {$C$};
%       \node[above] at (D) {$D$};
    \end{tikzpicture}
    
\end{document}

如果你更改此行:

\tdplotsetmaincoords{0}{90} % Set to {0}{90} to view only red

您只能看到朝上的(红色)面:

在此处输入图片描述

相关内容