![桌子,即 Tikz 中供人就餐的家具](https://linux22.com/image/235992/%E6%A1%8C%E5%AD%90%EF%BC%8C%E5%8D%B3%20Tikz%20%E4%B8%AD%E4%BE%9B%E4%BA%BA%E5%B0%B1%E9%A4%90%E7%9A%84%E5%AE%B6%E5%85%B7.png)
你会如何用 Tikz 画出一张桌子,也就是人们吃饭的家具,就像下面这样?一如既往,我非常感谢你巧妙的 tikz 构造!
答案1
这是一个粗略的解决方案。随着时间和重构,可能会写出一个优雅的解决方案。
关键部分是使用三维坐标系。
我只画了表格。
\begin{tikzpicture}[fill=lightgray]
%legs
\foreach \leg in {(0.3,0,0),(4.9,0,0),(4.9,0,-2.7),(0.3,0,-2.7)}{
\begin{scope}[shift=\leg,draw=red]
\draw[fill] (0,0,0) -- (0,0,-0.2) -- (0,2,-0.2) -- (0,2,0);
\draw[fill] (0,0,0) rectangle (-0.2,2,0);
\end{scope}}
%table
\begin{scope}[draw=blue,shift={(0,2,0)}]
\draw[fill] (0,0,0) -- (5,0,0) -- (5,0,-3) -- (0,0,-3) -- cycle;
\draw[fill] (0,0,0) -- (5,0,0) -- (5,-0.1,0)-- (0,-0.1,0) -- cycle;
\draw[fill] (5,0,0) -- (5,0,-3) -- (5,-0.1,-3)-- (5,-0.1,0) -- cycle;
\end{scope}
\end{tikzpicture}
结果是
Hera 是另一个版本的代码。它比较长,但它使用在开头定义的宏,以便于修改表格。
\begin{tikzpicture}[fill=lightgray]
%table dimensions
\pgfmathsetmacro{\tablelength}{5};
\pgfmathsetmacro{\tablewidth}{3};
\pgfmathsetmacro{\tablethickness}{0.2};
%leg dimensions
\pgfmathsetmacro{\leglength}{\tablewidth};
\pgfmathsetmacro{\halflegwidth}{0.5*\tablethickness};
\pgfmathsetmacro{\shift}{2*\halflegwidth};
%legs
\foreach \leg in {(0+\shift,0,0),(\tablelength-\shift,0,-\shift),
(\tablelength-\shift,0,-\tablewidth+\shift),
(0+\shift,0,-\tablewidth+\shift)}{
\path[shift=\leg,draw=red,fill]
(\halflegwidth,0,\halflegwidth) -- (\halflegwidth,0,-\halflegwidth) --
(\halflegwidth,\leglength,-\halflegwidth) --
(\halflegwidth,\leglength,\halflegwidth)
(\halflegwidth,0,\halflegwidth) rectangle
(-\halflegwidth,\leglength,\halflegwidth);
}
%table
\path[draw=blue,shift={(0,\leglength,0)},fill]
(0,0,0) -- (\tablelength,0,0) -- (\tablelength,0,-\tablewidth) --
(0,0,-\tablewidth) -- cycle
(0,0,0) -- (\tablelength,0,0) -- (\tablelength,-\tablethickness,0)--
(0,-\tablethickness,0) -- cycle
(\tablelength,0,0) -- (\tablelength,0,-\tablewidth) --
(\tablelength,-\tablethickness,-\tablewidth)--
(\tablelength,-\tablethickness,0) -- cycle;
\end{tikzpicture}