How to draw a cube completely filled with water, the other with water by half and the last leave it empty?
Something like this?
I defined a command \FillCube
with an optional argument taking values between 1
and 0
, controlling up to where the cube is filled. The default is 1
, meaning full (0
means empty).
The code:
\documentclass[varwidth=50cm,border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\Depth}{2}
\newcommand{\Height}{2}
\newcommand{\Width}{2}
\newcommand\FillCube[1][1]{
\coordinate (O) at (0,0,0);
\coordinate (A) at (0,\Width,0);
\coordinate (B) at (0,\Width,\Height);
\coordinate (C) at (0,0,\Height);
\coordinate (D) at (\Depth,0,0);
\coordinate (E) at (\Depth,\Width,0);
\coordinate (F) at (\Depth,\Width,\Height);
\coordinate (G) at (\Depth,0,\Height);
\ifx#10\relax
\else
\draw[fill=blue!40] (O) -- (C) -- (G) -- (D) -- cycle;
\fi
\draw[fill=gray!20,opacity=0.5] (O) -- (C) -- (G) -- (D) -- cycle;
\draw[fill=gray!20] (O) -- (A) -- (E) -- (D) -- cycle;
\draw[fill=blue!40] (O) -- ( $ (O)!#1!(A) $ ) -- ( $ (D)!#1!(E) $ ) -- (D) -- cycle;
\draw[fill=gray!20] (O) -- (A) -- (B) -- (C) -- cycle;
\draw[fill=blue!40] (O) -- ( $ (O)!#1!(A) $ ) -- ( $ (C)!#1!(B) $ ) -- (C) -- cycle;
\draw[fill=gray!20,opacity=0.4] (D) -- (E) -- (F) -- (G) -- cycle;
\draw[fill=blue!40,opacity=0.4] (D) -- ( $ (D)!#1!(E) $ ) -- ( $ (G)!#1!(F) $ ) -- (G) -- cycle;
\draw[fill=gray!20,opacity=0.6] (C) -- (B) -- (F) -- (G) -- cycle;
\draw[fill=blue!40,opacity=0.6] (C) -- ( $ (C)!#1!(B) $ ) -- ( $ (G)!#1!(F) $ ) -- (G) -- cycle;
\if#10\relax
\else
\draw[fill=blue!40,opacity=0.6] ( $ (O)!#1!(A) $ ) -- ( $ (C)!#1!(B) $ ) -- ( $ (G)!#1!(F) $ ) -- ( $ (D)!#1!(E) $ ) -- cycle;
\fi
}
\begin{document}
\begin{tikzpicture}
\foreach[count=\xi] \Valor in {1,0.8,...,0.2,0}
{
\begin{scope}[xshift=\xi*3cm]
\FillCube[\Valor]
\end{scope}
}
\end{tikzpicture}\par\bigskip
\begin{tikzpicture}
\FillCube
\begin{scope}[xshift=3cm]
\FillCube[0.5]
\end{scope}
\begin{scope}[xshift=6cm]
\FillCube[0]
\end{scope}
\end{tikzpicture}
\end{document}
An animation:
The code for the animation:
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\Depth}{2}
\newcommand{\Height}{2}
\newcommand{\Width}{2}
\newcommand<>\FillCube[1][1]{
\only#2{\coordinate (O) at (0,0,0);
\coordinate (A) at (0,\Width,0);
\coordinate (B) at (0,\Width,\Height);
\coordinate (C) at (0,0,\Height);
\coordinate (D) at (\Depth,0,0);
\coordinate (E) at (\Depth,\Width,0);
\coordinate (F) at (\Depth,\Width,\Height);
\coordinate (G) at (\Depth,0,\Height);
\ifx#10\relax
\else
\draw[fill=blue!40] (O) -- (C) -- (G) -- (D) -- cycle;
\fi
\draw[fill=gray!20,opacity=0.5] (O) -- (C) -- (G) -- (D) -- cycle;
\draw[fill=gray!20] (O) -- (A) -- (E) -- (D) -- cycle;
\draw[fill=blue!40] (O) -- ( $ (O)!#1!(A) $ ) -- ( $ (D)!#1!(E) $ ) -- (D) -- cycle;
\draw[fill=gray!20] (O) -- (A) -- (B) -- (C) -- cycle;
\draw[fill=blue!40] (O) -- ( $ (O)!#1!(A) $ ) -- ( $ (C)!#1!(B) $ ) -- (C) -- cycle;
\draw[fill=gray!20,opacity=0.4] (D) -- (E) -- (F) -- (G) -- cycle;
\draw[fill=blue!40,opacity=0.4] (D) -- ( $ (D)!#1!(E) $ ) -- ( $ (G)!#1!(F) $ ) -- (G) -- cycle;
\draw[fill=gray!20,opacity=0.6] (C) -- (B) -- (F) -- (G) -- cycle;
\draw[fill=blue!40,opacity=0.6] (C) -- ( $ (C)!#1!(B) $ ) -- ( $ (G)!#1!(F) $ ) -- (G) -- cycle;
\if#10\relax
\else
\draw[fill=blue!40,opacity=0.6] ( $ (O)!#1!(A) $ ) -- ( $ (C)!#1!(B) $ ) -- ( $ (G)!#1!(F) $ ) -- ( $ (D)!#1!(E) $ ) -- cycle;
\fi}
}
\begin{document}
\begin{frame}
\centering
\begin{tikzpicture}
\foreach[count=\xi] \Valor in {1,0.975,...,0.025,0}
{\FillCube<\xi>[\Valor]}
\end{tikzpicture}
\end{frame}
\end{document}
On a terminal (using ImageMagick):
convert -verbose -delay 12 -loop 0 -density 300 a.pdf a.gif
I adapted Gonzalo Medina's solution to use the TikZ perspective library.
Please pay attention to the changed axis directions.
The view point can be changed by the 3d view={az}{el}
option.
\documentclass[varwidth=50cm,border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{perspective}
\newcommand{\Depth}{2}
\newcommand{\Height}{2}
\newcommand{\Width}{2}
\newcommand\FillCube[1][1]{
\coordinate (O) at (0,0,0);
\coordinate (A) at (0,0,\Depth);
\coordinate (B) at (0,\Width,\Depth);
\coordinate (C) at (0,\Width,0);
\coordinate (D) at (\Height,0,0);
\coordinate (E) at (\Height,0,\Depth);
\coordinate (F) at (\Height,\Width,\Depth);
\coordinate (G) at (\Height,\Width,0);
\ifx#10\relax
\else
\draw[fill=blue!40] (O) -- (C) -- (G) -- (D) -- cycle;
\fi
\draw[fill=gray!20,opacity=0.5] (O) -- (C) -- (G) -- (D) -- cycle;
\draw[fill=gray!20,opacity=0.6] (O) -- (A) -- (E) -- (D) -- cycle;
\draw[fill=gray!20,opacity=0.6] (O) -- (A) -- (B) -- (C) -- cycle;
\draw[fill=gray!20,opacity=0.6] (C) -- (B) -- (F) -- (G) -- cycle;
\draw[fill=gray!20,opacity=0.6] (D) -- (E) -- (F) -- (G) -- cycle;
\draw[fill=blue!40,opacity=0.3] (O) -- ( $ (O)!#1!(A) $ ) -- ( $ (D)!#1!(E) $ ) -- (D) -- cycle;
\draw[fill=blue!40,opacity=0.3] (O) -- ( $ (O)!#1!(A) $ ) -- ( $ (C)!#1!(B) $ ) -- (C) -- cycle;
\draw[fill=blue!40,opacity=0.6] (C) -- ( $ (C)!#1!(B) $ ) -- ( $ (G)!#1!(F) $ ) -- (G) -- cycle;
\draw[fill=blue!40,opacity=0.6] (D) -- ( $ (D)!#1!(E) $ ) -- ( $ (G)!#1!(F) $ ) -- (G) -- cycle;
\if#10\relax
\else
\draw[fill=blue!40,opacity=0.6] ( $ (O)!#1!(A) $ ) -- ( $ (C)!#1!(B) $ ) -- ( $ (G)!#1!(F) $ ) -- ( $ (D)!#1!(E) $ ) -- cycle;
\fi}
\begin{document}
\begin{tikzpicture}[3d view={120}{15}]
\foreach[count=\xi] \Valor in {1,0.8,...,0.2,0}
{
\begin{scope}[xshift=\xi*3cm]
\FillCube[\Valor]
\end{scope}
}
\end{tikzpicture}\par\bigskip
\begin{tikzpicture}[3d view={120}{15}]
\FillCube
\begin{scope}[xshift=3cm]
\FillCube[0.5]
\end{scope}
\begin{scope}[xshift=6cm]
\FillCube[0]
\end{scope}
\end{tikzpicture}
\end{document}