Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I need to draw an axis-aligned bounding box containing a tilted rectangle for a report about physic engines.

My initial idea was to simply insert a rectangular node, apply a rotational transformation to it and then draw the bounding-box with the fit library of tikz. Unfortunately, this doesn't work.

I tinkered with the original idea and wrote the following code. The idea is that I insert four hidden nodes, corresponding to the corners of the rotated rectangle, and then I fit a box around them.

\begin{tikzpicture}

  % Draw the rectangle
  \coordinate (R) at (7,3);
  \node[rectangle,rotate=-30,minimum height=3cm, minimum width=2cm,inner sep=0pt,draw=black] (rect) at (R) {};

  % An invisible node at each corner of the rectangle
  \begin{scope}[rotate around={-30:(R)}]
    \path (R) node () {}
          ++(-1,-1.5) node (a) {}
          ++(2,0) node (b) {}
          ++(0,3) node (c) {}
          ++(-2,0) node (d) {};
    \end{scope}

  % Draw the bounding box
  \node[fit=(a) (b) (c) (d),draw=green,dashed] {};

\end{tikzpicture}

This is an image of the current result:

enter image description here

Is there an easier way to do that?

Bonus question: How could I get rid of the spaces between the rectangle and its bounding box (it want them to be touching)?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

You can directly use the corner nodes of the rect node, instead of defining them yourself.

If you just say fit=(rect), the fit command would assume that you mean (rect.north), (rect.east), etc., which doesn't help in this case because you need (rect.north east), (rect.south east), etc., but it's good to know anyway.

To get the outer rectangle to touch the inner one, set inner sep=0pt for the outer rectangle:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}
\begin{tikzpicture}

  % Draw the rectangle
  \coordinate (R) at (7,3);
  \node[rotate=-30,minimum height=3cm, minimum width=2cm,draw=black] (rect) at (R) {};


  % Draw the bounding box
  \node[fit=(rect.north west) (rect.north east) (rect.south east) (rect.south west), draw=green,dashed,inner sep=0pt] {};

\end{tikzpicture}
\end{document}

fit

share|improve this answer
    
Thank you a lot, that works perfectly! –  megamoustache May 28 '11 at 14:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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