我不明白在使用极坐标时如何通过指定的角度来解释它。我在我的MWE中准备了三个例子:
\documentclass{article}
\usepackage{tikz} % TikZ and PGF picture
\usetikzlibrary{intersections}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{matrix,arrows,decorations.pathmorphing}
\usetikzlibrary{positioning}
\newdimen\XCoord
\newdimen\YCoord
\newcommand*{\ExtractCoordinate}[1]{\path (#1); \pgfgetlastxy{\XCoord}{\YCoord};}%
\begin{document}
\begin{figure}[htp]
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (0,1);
\coordinate (C) at (1,1);
\node[left, color=blue] at (A) {A};
\node[left, color=blue] at (B) {B};
\node[right, color=blue] at (C) {C};
\draw (A) -- (B) -- (C);
\draw [color=cyan] let \p1=(A), \p2=(B), \p3=(C),
\n1={atan2(\y2-\y1,\x3-\x1)} in
(A) -- (\n1:2cm);
\ExtractCoordinate{B};
\node [below] at (1cm,-2cm) {B $(\XCoord,\YCoord)$};
\ExtractCoordinate{C};
\node [below] at (1cm,-2.5cm) {C $(\XCoord,\YCoord)$};
\end{tikzpicture}
\begin{tikzpicture}
\coordinate (A) at (-90.58205pt, 119.0348pt);
\coordinate (B) at (-90.58205pt, 133.26117pt);
\coordinate (C) at (-40.43698pt, 119.0348pt);
\node[left, color=blue] at (A) {A};
\node[left, color=blue] at (B) {B};
\node[right, color=blue] at (C) {C};
\draw (B) -- (A) -- (C);
\draw [color=cyan] let \p1=(A), \p2=(B), \p3=(C),
\n1={atan2(abs(\y2-\y1), abs(\x3-\x1))} in
(B) -- (74.161134732:2cm);
\draw[right, color=green] (B) -- (0:2cm);
\draw[right, color=red] (B) -- (45:2cm);
\ExtractCoordinate{B};
\node [below] at (1cm,-2cm) {B $(\XCoord,\YCoord)$};
\ExtractCoordinate{C};
\node [below] at (1cm,-2.5cm) {C $(\XCoord,\YCoord)$};
\end{tikzpicture}
\begin{tikzpicture}
\coordinate (A) at (2, 2);
\draw[right, color=green] (A) -- (0:2cm);
\draw[right, color=red] (A) -- (45:2cm);
\draw[right, color=blue] (A) -- (90:2cm);
\draw[right, color=black] (A) -- (74.161134732:2cm);
\end{tikzpicture}
\end{figure}
\end{document}
- 第一个例子:一切正常。
- 第二个例子:问题:
在以下命令中:
\draw [color=cyan] let \p1=(A), \p2=(B), \p3=(C), \n1={atan2(abs(\y2-\y1), abs(\x3-\x1))} in (B) -- (74.161134732:2cm);
我尝试计算角度,因此将 B 和 C 连接起来。不幸的是,没有成功。我认为 atan2 函数工作不正常,因此我用手动计算的角度替换变量 \n1 中的值,但我再次没有连接这两个点。
我得出结论,我不能正确使用极坐标,所以我尝试在第三个示例中测试所有内容。我预期绿线是水平的,但实际上,它是垂直的。
为了说明,我附上了一张图片:
你能解释一下我错在哪里吗?
答案1
你的代码中有两个错误。第一个是数学上的: atan2
:X然后是(我知道这在各个程序中有所不同,因此在使用atan2
函数时应始终检查这一点);此外,为了获得正确的角度,您不应采用绝对值,因为这至少会改变象限。因此,第二个示例中的正确语法atan2
应为:
\draw [color=cyan] let \p1=(A), \p2=(B), \p3=(C),
\n1={atan2(\x3-\x1, \y2-\y1)} in
(B) -- (\n:2cm);
这在您的第一个例子中没有出现的原因是因为两个坐标表达式的计算结果都与1
顺序无关,并且取绝对值不产生任何作用。
但是,上面的代码仍然不正确,这是第二个错误。你正在从(B)
到(\n:2cm)
画一条线。第二个位置指定在绝对坐标,因此是度数\n
和2cm
从起源。你希望它来自(B)
。要重新定位坐标系,您应该使用相对的坐标。这样(B) -- ++(\n:2cm)
你就得到了你想要的东西。
同样,这不会出现在第一个例子中,因为(A)
在这种情况下,您的兴趣点位于原点,因此相对和绝对坐标给出相同的答案。
这是您在第三个示例中犯的相同错误。坐标(0:2cm)
变为(2,0)
垂直向下(A)
(位于(2,2)
)。要获得水平线,您需要使其相对:++(0:2cm)
。
完整代码:
\documentclass{article}
\usepackage{tikz} % TikZ and PGF picture
\usetikzlibrary{intersections}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{matrix,arrows,decorations.pathmorphing}
\usetikzlibrary{positioning}
\newdimen\XCoord
\newdimen\YCoord
\newcommand*{\ExtractCoordinate}[1]{\path (#1);
\pgfgetlastxy{\XCoord}{\YCoord};}%
\begin{document}
\begin{figure}[htp]
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (0,1);
\coordinate (C) at (1,1);
\node[left, color=blue] at (A) {A};
\node[left, color=blue] at (B) {B};
\node[right, color=blue] at (C) {C};
\draw (A) -- (B) -- (C);
\draw [color=cyan] let \p1=(A), \p2=(B), \p3=(C),
\n1={atan2(\y2-\y1,\x3-\x1)} in
(A) -- (\n1:2cm);
\ExtractCoordinate{B};
\node [below] at (1cm,-2cm) {B $(\XCoord,\YCoord)$};
\ExtractCoordinate{C};
\node [below] at (1cm,-2.5cm) {C $(\XCoord,\YCoord)$};
\end{tikzpicture}
\begin{tikzpicture}
\coordinate (A) at (-90.58205pt, 119.0348pt);
\coordinate (B) at (-90.58205pt, 133.26117pt);
\coordinate (C) at (0pt, 119.0348pt);
\node[left, color=blue] at (A) {A};
\node[left, color=blue] at (B) {B};
\node[right, color=blue] at (C) {C};
\draw (B) -- (A) -- (C);
\draw [color=cyan] let \p1=(A), \p2=(B), \p3=(C),
\n1={atan2(\x3-\x1,\y1-\y2)} in
(B) -- ++(\n1:4cm);
\draw[right, color=green] (B) -- ++(0:2cm);
\draw[right, color=red] (B) -- ++(45:2cm);
\ExtractCoordinate{B};
\node [below] at (1cm,-2cm) {B $(\XCoord,\YCoord)$};
\ExtractCoordinate{C};
\node [below] at (1cm,-2.5cm) {C $(\XCoord,\YCoord)$};
\end{tikzpicture}
\begin{tikzpicture}
\coordinate (A) at (2, 2);
\draw[right, color=green] (A) -- ++(0:2cm);
\draw[right, color=red] (A) -- ++(45:2cm);
\draw[right, color=blue] (A) -- ++(90:2cm);
\draw[right, color=black] (A) -- ++(74.161134732:2cm);
\end{tikzpicture}
\end{figure}
\end{document}
结果: