7

I am trying to rotate a drawing around a specific point using PGF commands. As long as I hard code the center of rotation, this works just fine:

\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
  \draw[style=help lines] (0,0) grid[step=1cm] (3, 3);
  % Rotate around (1, 1).
  \pgftransformshift{\pgfpointxy{1}{1}}
  \pgftransformrotate{20}
  \pgftransformshift{\pgfpointxy{-1}{-1}}
  \draw (0.5, 0.5) -- (1.5, 1.5);
  \draw (1.5, 0.5) -- (0.5, 1.5);
\end{tikzpicture}
\end{document}

But I don't want to rotate around a hard coded center but around a computed one. I compute my center using \coordinate (x) at ($<some computation$);. I tried the following:

\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
  \draw[style=help lines] (0,0) grid[step=1cm] (3, 3);
  \coordinate (x) at (1, 1); % In real life: ($<computation>$)
  \pgftransformshift{\pgfpointscale{1}{\pgfpointanchor{x}{center}}}
  \pgftransformrotate{20}
  \pgftransformshift{\pgfpointscale{-1}{\pgfpointanchor{x}{center}}}
  \draw (0.5, 0.5) -- (1.5, 1.5);
  \draw (1.5, 0.5) -- (0.5, 1.5);
\end{tikzpicture}
\end{document}

Now what seems to happen is that the coordinate specified in the second shift is affected by the preceding \pgftransformrotate, even though I defined it before the transformations. Thus, the drawing is offset in the second example.

Can I somehow prevent my computed center of rotation from being transformed by the pgftransform commands? Or is there any other solution to my problem?

0

3 Answers 3

7

There are surely other solutions but the first one can be :

\documentclass[11pt]{scrartcl}
\usepackage{tikz}   
\usetikzlibrary{calc}
\begin{document}

\newdimen\myx
\newdimen\myy 

\begin{tikzpicture}
  \draw[style=help lines] (0,0) grid[step=1cm] (3, 3);
  \coordinate (x) at (1, 1); % In real life: ($<computation>$) 
  \pgfextractx{\myx}{\pgfpointanchor{x}{center}} 
  \pgfextracty{\myy}{\pgfpointanchor{x}{center}} 
  \draw[red] (0.5, 0.5) -- (1.5, 1.5) (1.5, 0.5) -- (0.5, 1.5); 
  \pgftransformshift{\pgfqpoint{\myx}{\myy}}  
  \pgftransformrotate{20}
  \pgftransformshift{\pgfqpoint{-\myx}{-\myy}}    
     \draw[blue] (0.5, 0.5) -- (1.5, 1.5) (1.5, 0.5) -- (0.5, 1.5);
\end{tikzpicture} 

\end{document}  

enter image description here

1
  • Great hint, thank you! I wrapped your solution into macros (see my answer) and it works like a charm.
    – hc_
    Commented Mar 23, 2012 at 14:39
4

First a simple TikZ solution:

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

\begin{tikzpicture}
  \draw[style=help lines] (0,0) grid[step=1cm] (3, 3);
  \coordinate (x) at (2, 2); % In real life: ($<computation>$)
\begin{scope}[rotate around={45:(x)}]
  \draw (0.5, 0.5) -- (1.5, 1.5);
  \draw (1.5, 0.5) -- (0.5, 1.5);
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

Second, the PGF version:

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

\begin{tikzpicture}
\draw[style=help lines] (0,0) grid[step=1cm] (3, 3);
\coordinate (x) at (2, 2); % In real life: ($<computation>$)
\node[fill,circle,inner sep=0.5mm] (O) at (x) {};
\pgftransformshift{\pgfpointanchor{x}{center}}
\pgfgetlastxy{\myx}{\myy}
\pgftransformrotate{20}
\pgftransformshift{\pgfpoint{-\myx}{-\myy}}%This has no effect if x used, x is the origin now
\draw (0.5, 0.5) -- (1.5, 1.5);
\draw (1.5, 0.5) -- (0.5, 1.5);
\end{tikzpicture}
\end{document}

enter image description here

2

I ended up using Altermundus' solution but to reduce clutter, I introduced two macros:

\def\savePoint#1#2{%
  \expandafter\newdimen\csname #2x\endcsname\expandafter\pgfextractx\csname #2x\endcsname{\pgfpointanchor{#1}{center}}
  \expandafter\newdimen\csname #2y\endcsname\expandafter\pgfextracty\csname #2y\endcsname{\pgfpointanchor{#1}{center}}}
\def\getPoint#1{\pgfpoint{\csname #1x\endcsname}{\csname #1y\endcsname}}

Now I write a little more compact code:

  \coordinate (x) at (1, 1);
  \savePoint{x}{x}
  \pgftransformshift{\pgfpointscale{1}{\getPoint{x}}}
  \pgftransformrotate{20}
  \pgftransformshift{\pgfpointscale{-1}{\getPoint{x}}}

Thanks for your help!

1
  • \pgfgetlastxy like percusse is another solution but you need to place it correctly. Commented Mar 23, 2012 at 16:11

You must log in to answer this question.

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