2
\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\begin{tikzpicture}
\begin{scope}[rotate=-{30}]
\draw (0,0)--(0,4)--(4,4)--(4,2)--(6,2)--(6,0)--(4,0)--(4,-2)--(2,-2)--(2,0)--cycle;
\draw (2, 0)--++(2,0);
\draw (0, 2)--++(4,0);
\draw (2,4)--++(0,-4);
\draw (4,2)--++(0,-2);
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

  1. What is the proper way to draw a rectangle around this shape so that it touches far left, right, top, bottom points?
  2. I have a feeling that I haven't constructed the non-regular shape in a systematic manner. How could I do it better?
  3. [Addendum upon Torbjørn's initial answer] How can I fill the bounding box, but exclude the shape inside? And how can I, say, draw a line from the shape to the bounding box.)
  • \draw (0,0) rectangle ++(2,2) (2,0) rectangle ++(2,2) (0,-2) rectangle ++(2,2) (2,-2) rectangle ++(2,2) (4,-2) rectangle ++(2,2) (2,-4) rectangle ++(2,2); also draws the shape. – user121799 Jun 4 '18 at 19:18
8

An easy way is to use the fit library along with a local bounding box.

enter image description here

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{fit}
\begin{document}
\begin{tikzpicture}
\begin{scope}[rotate=-{30},local bounding box=a]
\draw (0,0)--(0,4)--(4,4)--(4,2)--(6,2)--(6,0)--(4,0)--(4,-2)--(2,-2)--(2,0)--cycle;
\draw (2, 0)--++(2,0);
\draw (0, 2)--++(4,0);
\draw (2,4)--++(0,-4);
\draw (4,2)--++(0,-2);
\end{scope}
\node [fit=(a),inner sep=0pt,draw] {};
\end{tikzpicture}
\end{document}

Or, given the local bounding box, use a rectangle path:

\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\begin{tikzpicture}
\begin{scope}[rotate=-{30},local bounding box=a]
\draw (0,0) |- (4,4) |- (6,2) |- (4,0) |- (2,-2) |- cycle;
\draw (4, 0) -| (2,4)
      (0,2) -| (4,0);
\end{scope}
\draw (a.south west) rectangle (a.north east);
\end{tikzpicture}
\end{document}

(This shows the use of -|/|- paths to cut the number of coordinates in half.)

If drawing the same lines more than once isn't a problem, you could use a loop like this to draw the shape:

\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\begin{tikzpicture}
\begin{scope}[rotate=-{30},local bounding box=a]
\foreach \x/\y in {1/0,0/1,1/1,2/1,0/2,1/2} % list of rectangle centers
   \draw (\x-0.5, \y-0.5) rectangle ++(1,1);
\end{scope}
\draw (a.south west) rectangle (a.north east);
\end{tikzpicture}
\end{document}

Addendum

Perhaps the easiest way to achieve your additional requirements is to use nodes to make the squares, and a fit node to make the rectangle. By filling the nodes with white, and using on background layer to fill the fit node with some other colour, you get the appearance you're asking for. If you actually want to fill only the area between shape and rectangle you need to use \clip in a scope, and I'd actually suggest asking a new question.

Because everything is a node, drawing lines between inner squares and outer rectangle is easy, just use the node anchors as coordinates.

enter image description here

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{backgrounds,fit}
\begin{document}
\begin{tikzpicture}
\begin{scope}[rotate=-{30},local bounding box=a]
\foreach [count=\i] \x/\y in {1/0,0/1,1/1,2/1,0/2,1/2} % list of rectangle centers
   \node [transform shape, % allow for rotation
          draw, % draw outline
          fill=white, % fill with white
          minimum size=1cm, % set size
          name=n-\i,
          %label=center:n-\i % if you want to see the names of the nodes, uncomment this
          ] at (\x-0.5, \y-0.5) {};
\end{scope}
\scoped[on background layer] % the following path is on the background layer
  \node [draw=black, fill=blue!10, fit=(a), inner sep=0.2pt, name=frame] {};


\draw [-latex] (frame.280) to[bend right] (n-1);
\draw (frame.east) -- (n-3.north east);

\end{tikzpicture}
\end{document}
  • What would be the way to fill the rectangle but not the shape inside? – blackened Jun 5 '18 at 8:33
  • @blackened Depends a bit on what you mean exactly. Do you want to fill the area inside the rectangle, but outside the shape, or do you want to fill the entire rectangle, but have the shape on top of the filling? – Torbjørn T. Jun 5 '18 at 8:37
  • I want to fill the rectangle but exclude the shape inside. (Though, what I really want to understand is how the mechanism works.) – blackened Jun 5 '18 at 8:40
  • In fact, I have just realized that I also need to, say, draw a line from the shape inside to the rectangle itself? How can I do that? Shall I edit the question accordingly? – blackened Jun 5 '18 at 8:49
  • @blackened The best thing would be if you had realized that before posting the question in the first place, so you could have added all the requirements at once. As I did think of a fairly easy way of doing all that, I edited my answer though. – Torbjørn T. Jun 5 '18 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.