40

Is there a way in Tikz to translate and rotate a, lets say, rectangle by an arbitrary distance/angle? As an example

  1. Translation: 3 in x-direction, 5 in y-direction
  2. Rotation: 45 degree

Now my goal would be drawing a rectangle around the origin and then be able to first translate then rotate the object and vice versa via TikZ commands. So one could visually see, that the translation and the rotation do not commute.

The translate first then rotate is easily done by hand but the other way around I need the help of a calculator.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[scale = 0.5]
\draw [->](-5,0) -- (5,0); %x-axis
\draw [->](0,-5) -- (0,5); %y-axis
\draw[green] (1,1) -- (1,-1) -- (-1,-1) -- (-1,1) -- (1,1); %square around the origin
\draw[blue] ($ (3,{5+sqrt(2)}) $) -- ($ ({3-sqrt(2)},5) $) -- ($ (3,{5-sqrt(2)})  $) -- ($ ({3+sqrt(2)},5) $) -- ($ (3,{5+sqrt(2)}) $) ;  %Rotation first, then translated, values calculated by hand
\draw[green, rotate around={45:(0,0)}] (2,6) rectangle (4,4); %neat solution for the RT case
\end{tikzpicture}
\end{document}
1

2 Answers 2

39

There are several method, as describle in the section Coordinate Transformations in the pgfmanual.

One way is to set a coordinate transformation matrix directly, via cm:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[scale = 0.5]
  \draw [->](-5,0) -- (5,0); %x-axis
  \draw [->](0,-5) -- (0,5); %y-axis
  \draw[green] (1,1) -- (1,-1) -- (-1,-1) -- (-1,1) -- (1,1); %square around the origin
  \draw[blue,cm={cos(45) ,-sin(45) ,sin(45) ,cos(45) ,(3 cm,5 cm)}] (1,1) -- (1,-1) -- (-1,-1) -- (-1,1) -- (1,1);
\end{tikzpicture}
\end{document}  

resulting picture

Be careful, you need to use this with the right order the transformations. With cm, you don't have the choice, it's always the rotation and then the translation. And the pgfmanual says: "Usually, you do not use this option directly."

A different solution is to use shift and rotate:

\draw[blue,shift={(3 cm,5 cm)},rotate=45] (1,1) -- (1,-1) -- (-1,-1) -- (-1,1) -- (1,1); 
\draw[orange,rotate=45,shift={(3 cm,5 cm)}] (1,1) -- (1,-1) -- (-1,-1) -- (-1,1) -- (1,1); 

(Comment by Caramdir:) For this you need to remember that TikZ composes transformations in the opposite order than the naturally expected one (i.e. it acts on the coordinate plane, not on the objects).

resulting picture

6
  • 1
    Thanks a lot! I have a question about the command cm. Can you point me to a source where I can read up on it? The other way around it doesn't work, is that right? (Would be elegant if I could reach both TR and RT with the same command.
    – Philipp
    Commented Mar 23, 2012 at 21:07
  • @Philipp See the manual of PGF/TikZ. Just search for cm= in the PDF, and you'll find it. Commented Mar 23, 2012 at 21:18
  • @Philipp Be careful , you need to use with the good order the transformation With cmyou don't have the choice it's always the rotation and then the translation. And the pgfmanual says : "Usually, you do not use this option directly." Commented Mar 23, 2012 at 21:22
  • 4
    For the second solution one needs to remember that TikZ composes transformation in the opposite order than the naturally expected one (i.e. it acts on the coordinate plane, not on the objects).
    – Caramdir
    Commented Mar 23, 2012 at 21:25
  • @Caramdir yes you are right I need to add you remark at my answer. Commented Mar 23, 2012 at 21:28
33

for me, the best way is using scope

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}


\begin{tikzpicture}[scale = 0.5]
\draw [->](-5,0) -- (5,0); %x-axis
\draw [->](0,-5) -- (0,5); %y-axis
\draw[green] (1,1) -- (1,-1) -- (-1,-1) -- (-1,1) -- (1,1); %square around the origin

\begin{scope}[shift={(3,5)},rotate=45]
\draw[red] (1,1) -- (1,-1) -- (-1,-1) -- (-1,1) -- (1,1); %square around the origin
\end{scope}

\begin{scope}[shift={(-1.5,5.5)},rotate=45]
\draw[red] (1,1) -- (1,-1) -- (-1,-1) -- (-1,1) -- (1,1); %square around the origin
\end{scope}
\end{tikzpicture}
\end{document}

rotate-translate

3
  • 1
    Yes and No In this case you don't need a scope (except if you think that the syntax is better and if the code is easier to read) but a scope is useful if you want to apply something a whole set of graphic commands. Here a scope adds nothing. Remark \begin{scope}[shift={(-1.5,5.5)},rotate=45] is not equivalent to \begin{scope}[rotate=45,shift={(3,5)}]. Commented Mar 24, 2012 at 12:47
  • 1
    @AlainMatthes Would you mind elaborating on the difference you mentioned? Thanks!
    – tvk
    Commented Nov 1, 2016 at 22:50
  • Many thanks! I also think this is the most flexible, and this allowed me to transform several paths at once (I don't care much about the original question). Commented May 26, 2022 at 9:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .