I would like to create a graphic representing a stack of rectangles. I would ideally like a simple command like \stack{5} to create a stack of 5.
I can use the following code:
\draw[fill=blue] (0.4,-0.4) rectangle +(3,2);
\draw[fill=blue] (0.3,-0.3) rectangle +(3,2);
\draw[fill=blue] (0.2,-0.2) rectangle +(3,2);
\draw[fill=blue] (0.1,-0.1) rectangle +(3,2);
\draw[fill=blue] (0.0,-0.0) rectangle +(3,2);
However, it seems hard to turn that into a command, and if we need to shift the stack around it is a hassle to change all of the coordinates. I was thinking this might be better:
\draw[fill=blue] (0.4,-0.4)
rectangle +(3,2) ++(-0.1,0.1)
rectangle +(3,2) ++(-0.1,0.1)
rectangle +(3,2) ++(-0.1,0.1)
rectangle +(3,2) ++(-0.1,0.1)
rectangle +(3,2) ++(-0.1,0.1);
This has the advantage that shifting it around only changes the coordinates in one obvious place, and it is even a little more clear about what is intended.
Unfortunately, this second version fills all the rectangles first, and then draws their outlines, so the "top" rectangle does not cover the rest of the stack.
Is there a simple way to draw overlapping regions using relative draw commands?
Best Answer
Here's a solution that exploits the fact that paths constructed using the
edge
command are separate and thus are drawn separately. Anedge
path is as flexible as ato
path so can be adapted to draw just about anything, including a rectangle. So we define a newto path
and anedge
style that invokes. it.This results in the following: