0

I would be grateful to know how to:

  1. fix the code to make it work
  2. be able to control the viewing angle so that I can see the rectangle like the top surface of this image

enter image description here

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

Diaa
  • 9,599

2 Answers2

2

Like this:

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

enter image description here

Tom
  • 7,318
  • 4
  • 21
2

Like this:

enter image description here

Code:

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

If You change this line:

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

You see only the up (red) face:

enter image description here