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 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?

share|improve this question
add comment

3 Answers

up vote 6 down vote accepted

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

share|improve this answer
    
Great hint, thank you! I wrapped your solution into macros (see my answer) and it works like a charm. –  hc_ Mar 23 '12 at 14:39
add comment

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!

share|improve this answer
    
\pgfgetlastxy like percusse is another solution but you need to place it correctly. –  Alain Matthes Mar 23 '12 at 16:11
add comment

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

share|improve this answer
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.