请考虑以下 MWE:
\documentclass[]{article}
\usepackage{tikz}
\begin{document}
\typeout{Using internal functions}
\begin{tikzpicture}[
color=green!50!black]
\draw[thin, dotted] (0,0) grid (2,2);
\draw (0,0) to (2,2) \pgfextra{%
\typeout{\tikztostart --- \tikztotarget}
\pgfmathanglebetweenpoints{(\tikztostart)}{(\tikztotarget)}\xdef\aaa{\pgfmathresult}};
\node [draw] at (1,1) {\aaa}; % shouldn't be 45?
\end{tikzpicture}
\quad
\typeout{Using coordinates}
\begin{tikzpicture}[
color=blue]
\draw[thin, dotted] (0,0) grid (2,2);
\draw (0,0) coordinate(a) to (2,2) coordinate(b) \pgfextra{%
\typeout{\tikztostart --- \tikztotarget}
\pgfmathanglebetweenpoints{\pgfpointanchor{a}{center}}{\pgfpointanchor{b}{center}}\xdef\aaa{\pgfmathresult}};
\node [draw, rotate=\aaa] at (1,1) {\aaa}; % Ok, now it's 45
\end{tikzpicture}
\quad
\typeout{Using coordinates and scaling}
\begin{tikzpicture}[
color=red, xscale=2
]
\draw[thin, dotted] (0,0) grid (2,2);
\draw (0,0) coordinate(a) to (2,2) coordinate(b) \pgfextra{%
\typeout{\tikztostart --- \tikztotarget}
\pgfmathanglebetweenpoints{\pgfpointanchor{a}{center}}{\pgfpointanchor{b}{center}}\xdef\aaa{\pgfmathresult}};
\node [draw, rotate=\aaa] at (1,1) {\aaa}; % Why?
\end{tikzpicture}
\quad
\typeout{Using coordinates and rotating}
\begin{tikzpicture}[
color=black, rotate=-45
]
\draw[thin, dotted] (0,0) grid (2,2);
\draw (0,0) coordinate(a) to (2,2) coordinate(b) \pgfextra{%
\typeout{\tikztostart --- \tikztotarget}
\pgfmathanglebetweenpoints{\pgfpointanchor{a}{center}}{\pgfpointanchor{b}{center}}\xdef\aaa{\pgfmathresult}};
\node [draw, rotate=\aaa] at (1,1) {\aaa}; % Why?
\end{tikzpicture}
\end{document}
结果是:
并且在日志中我有(预期):
Using internal functions
0.0pt,0.0pt--- 2,2
Using coordinates
0.0pt,0.0pt--- 2,2
Using coordinates and scaling
0.0pt,0.0pt--- 2,2
Using coordinates and rotating
0.0pt,0.0pt--- 2,2
因此,第一个(绿色)不起作用(我尝试添加括号,但(\tikztostart)
仍然……)。
蓝色技巧有效,我可以正确获取角度并绘制它。
但是,如果我以非对称方式缩放(或使用负比例因子)并进行全局旋转,它就会失败。我理解为什么计算出的角度仍然是 45 度(我们处于旋转/缩放的坐标系中),但似乎rotate
节点的是在绝对坐标中。
我不想使用transform shape
,因为结果是(预期的,正确的):
是否可以计算路径角度以便它适用于红色和黑色的情况而不使用该transform shape
选项?
我想法我找到了一种方法,但我不确定我在这里做的是否正确,所以我发布它以征求意见。看(我删除了@AndréC 评论的第一件事):
\documentclass[]{article}
\usepackage{tikz}
\makeatletter
\newdimen{\@@xa}
\newdimen{\@@ya}
\newdimen{\@@xb}
\newdimen{\@@yb}
\def\computerot{%
\pgfgettransformentries\a\b\c\d\temp\temp
\pgfextractx{\@@xa}{\pgfpointanchor{a}{center}}
\pgfextracty{\@@ya}{\pgfpointanchor{a}{center}}
\pgfextractx{\@@xb}{\pgfpointanchor{b}{center}}
\pgfextracty{\@@yb}{\pgfpointanchor{b}{center}}
\typeout{XA:\space\the\@@xa\space YA:\space\the\@@ya\space XB:\space\the\@@xb\space YB:\space\the\@@yb}
\typeout{MATRIX\space A:\a\space B:\b\space C:\c\space D:\d}
\pgfmathsetmacro{\newx}{\a*(\@@xb-\@@xa)+\b*(\@@yb-\@@ya)}
\pgfmathsetmacro{\newy}{\c*(\@@xb-\@@xa)+\d*(\@@yb-\@@ya)}
\typeout{NEWX:\space\newx\space NEWY:\space\newy}
\pgfmathsetmacro{\rot}{atan2(\c*(\@@xb-\@@xa)+\d*(\@@yb-\@@ya), \a*(\@@xb-\@@xa)+\b*(\@@yb-\@@ya) )}
\typeout{ROT\space\rot}
}
\makeatother
\begin{document}
\typeout{Using coordinates}
\begin{tikzpicture}[
color=blue]
\draw[thin, dotted] (0,0) grid (2,2);
\draw (0,0) coordinate(a) to (2,2) coordinate(b) \pgfextra{%
\typeout{\tikztostart --- \tikztotarget}
\computerot
\xdef\aaa{\rot}
};
\node [draw, rotate=\aaa] at (1,1) {\aaa}; % Ok, now it's 45
\end{tikzpicture}
\quad
\typeout{Using coordinates and scaling}
\begin{tikzpicture}[
color=red, xscale=2,
]
\draw[thin, dotted] (0,0) grid (2,2);
\draw (0,0) coordinate(a) to (2,2) coordinate(b) \pgfextra{%
\typeout{\tikztostart --- \tikztotarget}
\computerot
\xdef\aaa{\rot}
};
\node [draw, rotate=\aaa] at (1,1) {\aaa}; % Why?
\end{tikzpicture}
\quad
\typeout{Using coordinates and rotating}
\begin{tikzpicture}[
color=black, rotate=30,
]
\draw[thin, dotted] (0,0) grid (2,2);
\draw (0,0) coordinate(a) to (2,2) coordinate(b) \pgfextra{%
\typeout{\tikztostart --- \tikztotarget}
\computerot
\xdef\aaa{\rot}
};
\node [draw, rotate=\aaa] at (1,1) {\aaa}; % Why?
\end{tikzpicture}
\end{document}
这似乎适用于具有不同缩放比例的情况(其中角度大约 26 度是正确的),但不适用于旋转:我有 15 度,而我应该有 75 度或大约这个数字......
输出为:
Using coordinates
0.0pt,0.0pt--- 2,2
XA: 0.0pt YA: 0.0pt XB: 56.90549pt YB: 56.90549pt
MATRIX A:1.0 B:0.0 C:0.0 D:1.0
NEWX: 56.90549 NEWY: 56.90549
ROT 45.0
Using coordinates and scaling
0.0pt,0.0pt--- 2,2
XA: 0.0pt YA: 0.0pt XB: 56.90549pt YB: 56.90549pt
MATRIX A:2.0 B:0.0 C:0.0 D:1.0
NEWX: 113.81097 NEWY: 56.90549
ROT 26.56505
Using coordinates and rotating
0.0pt,0.0pt--- 2,2
XA: 0.0pt YA: 0.0pt XB: 56.9057pt YB: 56.90572pt
MATRIX A:0.86603 B:0.5 C:-0.5 D:0.86603
NEWX: 77.73476 NEWY: 20.82907
ROT 15.00056
并且看起来,为了进行旋转,X 和 Y 坐标有所交换。
答案1
买得好,罗马诺!
\documentclass[]{article}
\usepackage{tikz}
\tikzset{good bye/.code=\pgftransformreset}
\begin{document}
\typeout{Using coordinates and scaling}
\begin{tikzpicture}[
color=red, xscale=2,
]
\draw[thin, dotted] (0,0) grid (2,2);
\draw (0,0) coordinate(a) to (2,2) coordinate(b);
\path[good bye] (a) -- (b) node[midway,sloped,draw] {pft};
\end{tikzpicture}
\quad
\typeout{Using coordinates and rotating}
\begin{tikzpicture}[
color=black, rotate=-45
]
\draw[thin, dotted] (0,0) grid (2,2);
\draw (0,0) coordinate(a) to (2,2) coordinate(b);
\path[good bye] (a) -- (b) node[midway,sloped,draw] {pft};
\end{tikzpicture}
\end{document}
让您更快乐。我强调,这没有必要那么复杂,也就是说,对于所有目的,我都能想象有更简单的解决方案。
\documentclass[]{article}
\usepackage{tikz}
\makeatletter
\newdimen{\@@xa}
\newdimen{\@@ya}
\newdimen{\@@xb}
\newdimen{\@@yb}
\def\computerot{%
\pgfgettransformentries\a\b\c\d\temp\temp
\pgfextractx{\@@xa}{\pgfpointanchor{a}{center}}
\pgfextracty{\@@ya}{\pgfpointanchor{a}{center}}
\pgfextractx{\@@xb}{\pgfpointanchor{b}{center}}
\pgfextracty{\@@yb}{\pgfpointanchor{b}{center}}
\typeout{XA:\space\the\@@xa\space YA:\space\the\@@ya\space XB:\space\the\@@xb\space YB:\space\the\@@yb}
\typeout{MATRIX\space A:\a\space B:\b\space C:\c\space D:\d}
\pgfmathsetmacro{\newx}{\a*(\@@xb-\@@xa)+\b*(\@@yb-\@@ya)}
\pgfmathsetmacro{\newy}{\c*(\@@xb-\@@xa)+\d*(\@@yb-\@@ya)}
\typeout{NEWX:\space\newx\space NEWY:\space\newy}
\pgfmathsetmacro{\rot}{atan2(\c*(\@@xb-\@@xa)+\d*(\@@yb-\@@ya), \a*(\@@xb-\@@xa)+\b*(\@@yb-\@@ya) )}
\typeout{ROT\space\rot}
}
\makeatother
\tikzset{good bye/.code=\pgftransformreset\computerot%
\xdef\aaa{\rot}%
}
\begin{document}
\typeout{Using coordinates and scaling}
\begin{tikzpicture}[
color=red, xscale=2,
]
\draw[thin, dotted] (0,0) grid (2,2);
\draw (0,0) coordinate(a) to (2,2) coordinate(b)
[good bye] (a) -- (b) node[midway,rotate=\aaa,draw] {pft};
\end{tikzpicture}
\quad
\typeout{Using coordinates and rotating}
\begin{tikzpicture}[
color=black, rotate=-45
]
\draw[thin, dotted] (0,0) grid (2,2);
\draw (0,0) coordinate(a) to (2,2) coordinate(b)
[good bye] (a) -- (b) node[midway,rotate=\aaa,draw] {pft};
\end{tikzpicture}
\end{document}