6

I'm trying to prepare a worksheet for my students on calculating the volume of a rectangular prism (cuboid). Using the code below I manage to generate the rectangular prism, but how do I add the dimensions? I'd like to have, for example, "4cm" for the length, placed just under the length line, "4cm" for the width, placed below right of the width line, and "6cm" for the height, placed to the right of the height line.

  1. what is the code for doing that?
  2. Generally speaking, how do I position these dimensions where I like? I may want to rotate the prism and place the dimensions above, below, center, right or left of the prism outlines. How do I do that?

My code:

\begin{tikzpicture}
\pgfmathsetmacro{\x}{1}
\pgfmathsetmacro{\y}{1}
\pgfmathsetmacro{\z}{1.5}
\path (0,0,\y) coordinate (A) (\x,0,\y) coordinate (B) (\x,0,0) coordinate (C) (0,0,0)
coordinate (D) (0,\z,\y) coordinate (E) (\x,\z,\y) coordinate (F) (\x,\z,0) coordinate (G)
(0,\z,0) coordinate (H);
\draw (A)--(B)--(C)--(G)--(F)--(B) (A)--(E)--(F)--(G)--(H)--(E);
\draw [black] (A)--(D)--(C) (D)--(H);
\end{tikzpicture}
10
\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}[>=latex,scale=2]
\pgfmathsetmacro{\x}{1}
\pgfmathsetmacro{\y}{1}
\pgfmathsetmacro{\z}{1.5}
\path (0,0,\y) coordinate (A) (\x,0,\y) coordinate (B) (\x,0,0) coordinate (C) (0,0,0)
coordinate (D) (0,\z,\y) coordinate (E) (\x,\z,\y) coordinate (F) (\x,\z,0) coordinate (G)
(0,\z,0) coordinate (H);
\draw (A)--(B)--(C)--(G)--(F)--(B) (A)--(E)--(F)--(G)--(H)--(E);
\draw (A)--(D)--(C) (D)--(H);

\draw[thin,|<->|] ($(A)+(0,-4pt)$) -- node[below]{4cm}($(B)+(0,-4pt)$);
\draw[thin,|<->|] ($(B)+(-45:4pt)$) -- node[below,sloped]{4cm}($(C)+(-45:4pt)$);
\draw[thin,|<->|] ($(C)+(4pt,0)$) -- node[below,sloped]{6cm}($(G)+(4pt,0)$);

\end{tikzpicture}

\end{document}

enter image description here

3

To start with, you might want to view the position of the coordinated. That can be done by

\begin{tikzpicture}
\pgfmathsetmacro{\x}{1}
\pgfmathsetmacro{\y}{1}
\pgfmathsetmacro{\z}{1.5}
\path (0,0,\y) coordinate (A) (\x,0,\y) coordinate (B) (\x,0,0) coordinate (C) (0,0,0)
coordinate (D) (0,\z,\y) coordinate (E) (\x,\z,\y) coordinate (F) (\x,\z,0) coordinate (G)
(0,\z,0) coordinate (H);
\draw (A)--(B)--(C)--(G)--(F)--(B) (A)--(E)--(F)--(G)--(H)--(E);
\draw [black] (A)--(D)--(C) (D)--(H);
\foreach \coor in {A,B,...,H}{%
  \node[above] at (\coor){\coor};
}
\end{tikzpicture}

enter image description here

With that it is easier to include the text as nodes in the drawing (I also dashed the hidden lines):

\begin{tikzpicture}
\pgfmathsetmacro{\x}{1}
\pgfmathsetmacro{\y}{1}
\pgfmathsetmacro{\z}{1.5}
\path (0,0,\y) coordinate (A) (\x,0,\y) coordinate (B) (\x,0,0) coordinate (C) (0,0,0)
coordinate (D) (0,\z,\y) coordinate (E) (\x,\z,\y) coordinate (F) (\x,\z,0) coordinate (G)
(0,\z,0) coordinate (H);
\draw (A)-- node[below]{4cm} (B)-- node[below,sloped]{4cm} (C)--(G)--(F)--(B) (A)-- node[above,sloped]{6cm}(E)--(F)--(G)--(H)--(E);
\draw [dashed,black] (A)--(D)--(C) (D)--(H);
\end{tikzpicture}

enter image description here

The rotation can be done by changing the coordinate system basis vectirs. Tikz plots the lines on a 2-dimensional space, but you can use 3-dimensional vectors (projected to 2-dim). (Since the coordinates in the example and tikz are using different orders of y and z, there is a slight confusion in the code below):

\begin{tikzpicture}
\draw[->](0,0,0) -- (1,0,0) node[pos=1.2]{$x$};
\draw[->](0,0,0) -- (0,1,0) node[pos=1.2]{$z$};
\draw[->](0,0,0) -- (0,0,1) node[pos=1.2]{$y$};
\end{tikzpicture}
\begin{tikzpicture}[x={(0.7cm,-0.7cm)},y={(0.2cm,0.7cm)},z={(-0.7cm,-0.7cm)}]
\draw[->](0,0,0) -- (1,0,0) node[pos=1.2]{$x$};
\draw[->](0,0,0) -- (0,1,0) node[pos=1.2]{$z$};
\draw[->](0,0,0) -- (0,0,1) node[pos=1.2]{$y$};
\end{tikzpicture}

enter image description here

To rotate the prism one can use:

\begin{tikzpicture}[x={(0.7cm,-0.7cm)},y={(0.2cm,0.7cm)},z={(-0.7cm,-0.7cm)}]
\pgfmathsetmacro{\x}{1}
\pgfmathsetmacro{\y}{1}
\pgfmathsetmacro{\z}{1.5}
\path (0,0,\y) coordinate (A) (\x,0,\y) coordinate (B) (\x,0,0) coordinate (C) (0,0,0)
coordinate (D) (0,\z,\y) coordinate (E) (\x,\z,\y) coordinate (F) (\x,\z,0) coordinate (G)
(0,\z,0) coordinate (H);
\draw (A)-- node[below,sloped,]{4cm} (B)-- node[below,sloped]{4cm} (C)--(G)--(F)--(B) (A)-- node[above,sloped]{6cm}(E)--(F)--(G)--(H)--(E);
\draw [dashed,black] (A)--(D)--(C) (D)--(H);
\end{tikzpicture}

enter image description here

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.