How would you draw a table, i.e. the piece of furniture on which one eats, like the following in Tikz? As always, I am very grateful for your clever tikz constructions!
-
2There is a website texample.net which lists a lot of TikZ examples. Have a look if you find what you are looking for there. If not it would be a good idea to upload the solution to your drawing request there, so other people find them more easily.– Martin ScharrerApr 4, 2011 at 7:49
-
Thanks, Martin, I plan on doing just that at the end of the project on which Im currently working! From what I understand, the license under which answers appear here allows this kind of cross-posting as long as I link back to the original answer.– Thanos D. PapaïoannouApr 4, 2011 at 22:47
1 Answer
Here is a crude solution. With time and refactoring, an elegant solution may be written up.
The key part is to use the 3-dimensional coordinate system.
I only drew the table.
\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}
The result is
Hera is another version of the code. It is longer, but it uses macros defined at the beginning, to make it easier to modify the table.
\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}