I have the following code

\documentclass{article}
\usepackage{graphicx,tikz}
\begin{document}
\begin{tikzpicture}
\draw[ultra thick] (-1,0) -- (1,0);
\draw[ultra thick] (0,0) -- (0,2);
\end{tikzpicture}
\rotatebox{115}{\begin{tikzpicture}
\draw[ultra thick] (-1,0) -- (1,0);
\draw[ultra thick] (0,0) -- (0,2);
\end{tikzpicture}}
\end{document} 

giving

enter image description here

The picture is supposed to represent the two possible states after throwing a (stylized) nail. The angle in the \rotatebox was obtained by trial and error. I was wondering if I could use tikz to do the same job without guessing.

up vote 5 down vote accepted

Yes, TikZ can do that.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[ultra thick] (-1,0) -- (1,0);
\draw[ultra thick] (0,0) -- (0,2);
\end{tikzpicture}
\begin{tikzpicture}[rotate={atan2(1,2)+90}]
\draw[ultra thick] (-1,0) -- (1,0);
\draw[ultra thick] (0,0) -- (0,2);
\end{tikzpicture}
\end{document} 

enter image description here

OK, let's let TikZ do the calculation.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\draw[ultra thick] (-1,0) -- (1,0);
\draw[ultra thick] (0,0) -- (0,2);
\end{tikzpicture}
\begin{tikzpicture}[globalize angle/.code={\xdef#1{\n1}}]
% the points (0,2) and (-1,0) are the ones you want to be horizontal
\path let \p1=($(0,2)-(-1,0)$),\n1={180-atan2(\y1,\x1)} in 
[globalize angle=\myangle];
\begin{scope}[rotate=\myangle]
\draw[ultra thick] (-1,0) -- (1,0);
\draw[ultra thick] (0,0) -- (0,2) coordinate(aux);
\end{scope}
\end{tikzpicture}
\end{document} 
  • 1
    +1, but instead of "tikz can do that", it should be "marmots know trigonometry" :) – samcarter Oct 22 at 14:39
  • @marmot Quite nice/ Will accept it when I can. Never looked at page 933 of the pgfmanual before! – Denis Oct 22 at 14:40
  • 1
    @samcarter OK OK, I added a second possibility in which calc does the computation. ;-) And of course marmots need to know trigonometry, otherwise they'd get lost in their burrows. ;-) – marmot Oct 22 at 14:41
  • Wow! That is an interesting approach! – samcarter Oct 22 at 14:51
  • @samcarter Both options are basically the same except that I subtracted the vectors by hand in the first proposal (and there is an exchange of x and y, which is however conceptually irrelevant). – marmot Oct 22 at 15:17

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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