我正在尝试使用 PGF 命令围绕特定点旋转图形。只要我对旋转中心进行硬编码,就可以正常工作:
\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}
但我不想围绕硬编码中心旋转,而是围绕计算出的中心旋转。我使用 计算中心\coordinate (x) at ($<some computation$);
。我尝试了以下方法:
\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}
\pgftransformrotate
现在似乎发生的情况是,尽管我在变换之前定义了它,但第二个移位中指定的坐标仍受到前一个的影响。因此,在第二个示例中,绘图发生了偏移。
我能以某种方式阻止命令转换计算出的旋转中心pgftransform
吗?或者还有其他方法可以解决我的问题吗?
答案1
当然还有其他解决方案,但第一个可以是:
\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}
答案2
首先是一个简单的 TikZ 解决方案:
\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}
二、PGF版本:
\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}
答案3
我最终使用了 Altermundus 的解决方案,但为了减少混乱,我引入了两个宏:
\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}}
现在我写一些更紧凑的代码:
\coordinate (x) at (1, 1);
\savePoint{x}{x}
\pgftransformshift{\pgfpointscale{1}{\getPoint{x}}}
\pgftransformrotate{20}
\pgftransformshift{\pgfpointscale{-1}{\getPoint{x}}}
感谢您的帮助!