我想画3个立方体,一个装满水,一个半满的,一个完全空的。我可以画出来,但我做不到满的、空的、半满的三种形式的印象。
\documentclass{article}
\usepackage{tikz}
\usepackage{xcolor}
\begin{document}
\newcommand{\Depth}{2}
\newcommand{\Height}{2}
\newcommand{\Width}{2}
\begin{tikzpicture}
\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);
\draw[blue,fill=gray!20] (O) -- (C) -- (G) -- (D) -- cycle;
\draw[blue,fill=gray!20] (O) -- (A) -- (E) -- (D) -- cycle;
\draw[blue,fill=gray!10] (O) -- (A) -- (B) -- (C) -- cycle;
\draw[gray,fill=gray20,opacity=0.4] (D) -- (E) -- (F) -- (G) -- cycle;
\draw[blue,fill=gray!20,opacity=0.6] (C) -- (B) -- (F) -- (G) -- cycle;
\draw[blue,fill=gray!20,opacity=0.8] (A) -- (B) -- (F) -- (E) -- cycle;
\end{tikzpicture}
\qquad
\begin{tikzpicture}
\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);
\draw[fill=whitegray!20] (O) -- (C) -- (G) -- (D) -- cycle;
\draw[fill=gray!20] (O) -- (A) -- (E) -- (D) -- cycle;
\draw[fill=gray!10] (O) -- (A) -- (B) -- (C) -- cycle;
\draw[fill=gray20,opacity=0.4] (D) -- (E) -- (F) -- (G) -- cycle;
\draw[fill=gray!5,opacity=0.6] (C) -- (B) -- (F) -- (G) -- cycle;
\draw[fill=gray!5,opacity=0.8] (A) -- (B) -- (F) -- (E) -- cycle;
\end{tikzpicture}
\qquad
\begin{tikzpicture}
\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);
\draw[fill=whitegray!20] (O) -- (C) -- (G) -- (D) -- cycle;
\draw[fill=gray!20] (O) -- (A) -- (E) -- (D) -- cycle;
\draw[fill=gray!10] (O) -- (A) -- (B) -- (C) -- cycle;
\draw[fill=gray20,opacity=0.4] (D) -- (E) -- (F) -- (G) -- cycle;
\draw[fill=gray!5,opacity=0.6] (C) -- (B) -- (F) -- (G) -- cycle;
\draw[fill=gray!5,opacity=0.8] (A) -- (B) -- (F) -- (E) -- cycle;
\end{tikzpicture}
\end{document}
答案1
像这样吗?
我定义了一个命令\FillCube
,其中有一个可选参数,取值在1
和之间0
,控制立方体的填充位置。默认值为1
,表示已满(0
表示为空)。
代码:
\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}
动画:
动画的代码:
\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}
在终端上(使用 ImageMagick):
convert -verbose -delay 12 -loop 0 -density 300 a.pdf a.gif
答案2
我改编了 Gonzalo Medina 的解决方案以使用 TikZ 透视库。请注意更改的轴方向。可以通过选项更改视点3d view={az}{el}
。
\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}