坐标 A、B:计算 |BA| 以及 +x 与 (BA) 之间的角度

坐标 A、B:计算 |BA| 以及 +x 与 (BA) 之间的角度

这个问题分为两部分:

  1. 为什么计算的角度总是 0(应该是 45)

  2. 计算坐标之间距离的直接方法是什么(有如何计算 TikZ 中两点之间的距离,尽管我希望得到更简单的事情,可能通过定义\coordinate两个点并获取它们的距离。

我想到的应用是定义一个scope局部坐标系,其原点为A,局部 +x 轴的方向为B-A。如果有更简单的方法,我会很高兴发现它。

\documentclass{minimal}
\usepackage{tikz}
\begin{document}
    \tikz{
        \def\A{(1,1)}
        \def\B{(2,2)}
        %% {1} this returns 0.0 although it should be 1.414213...
        \pgfmathanglebetweenpoints{(1,0)}{\B-\A}
        \let\abAngle\pgfmathresult
        %% {2} how to compute eyclidean distance of coordinates?
        %% we would have to extract x and y components of B-A to be able to use \pgfveclen{}{}
        %\pgfveclen{??}{??}
        \let\abLength\pgfmathresult
        \message{|\B-\A| = \abLength, angle between +x and (\B-\A) = \abAngle}
    }
\end{document}

结果如下:

|(2,2)-(1,1)| = 0.0, angle between +x and ((2,2)-(1,1)) = 0.0

答案1

另一种计算长度和角度的方法。可以使用\pgfmathanglebetweenpoints坐标或节点。您需要使用\pgfpointanchor来获取节点的名称。

\documentclass{scrartcl}
\usepackage{tikz} 

\makeatletter      
\newcommand{\getLengthAndAngle}[2]{%
    \pgfmathanglebetweenpoints{\pgfpointanchor{#1}{center}}
                              {\pgfpointanchor{#2}{center}}
    \global\let\myangle\pgfmathresult % we need a global macro 
    \pgfpointdiff{\pgfpointanchor{#1}{center}}
                 {\pgfpointanchor{#2}{center}}
    \pgf@xa=\pgf@x % no need to use a new dimen
    \pgf@ya=\pgf@y
    \pgfmathparse{veclen(\pgf@xa,\pgf@ya)/28.45274} % to convert from pt to cm   
    \global\let\mylength\pgfmathresult % we need a global macro
}
\makeatother 

\begin{document}
\begin{tikzpicture}

\coordinate (A) at (0,0);
\coordinate (B) at (3,4);
% we get the  length and the angle between A and B
\getLengthAndAngle{A}{B}
% to test 
\draw (0,0) -- (\myangle:\mylength);
% to use in a scope
\begin{scope}[shift={(\myangle:\mylength)},rotate=\myangle]
\draw[thick,red] (0,0) -- ++(-90:2);
\end{scope}

\end{tikzpicture}
\end{document}

更新最简单的解决方案:

我们可以用 ($(B)-(A)$) 和库来避免计算 AB 的长度calc。提取角度就足够了。我定义了\pgfextractangle使用相同语法来获取角度。

\documentclass{scrartcl}
\usepackage{tikz} 
\usetikzlibrary{calc}

\newcommand{\pgfextractangle}[3]{%
    \pgfmathanglebetweenpoints{\pgfpointanchor{#2}{center}}
                              {\pgfpointanchor{#3}{center}}
    \global\let#1\pgfmathresult  
}

\begin{document}
\begin{tikzpicture}

\coordinate (A) at (0,0);
\coordinate (B) at (3,4);

\pgfextractangle{\angle}{A}{B}
\draw (A) -- (B);

\begin{scope}[shift={($(B)-(A)$)},rotate=\angle]
\draw[thick,red] (0,0) -- ++(-90:3);
\end{scope}

\end{tikzpicture} 

\end{document}

在此处输入图片描述

答案2

修复了代码并应用了 Altermundus 的精彩代码片段来删除多余的寄存器!

我不明白为什么链接的问题不是答案(也许你稍后会详细说明),但这是一个pgf基于 - 的答案。顺便说一句,代码远非正确,更不用说最佳了,但我想尽可能慢地完成它。

第一个问题的答案是:\pgfmathanglebetweenpoints接受point而不是coordinate。因此,如果您提供,\pgfpoint{}{}它就会按预期工作。

另外,我猜下面的代码与你想要的scope选项很接近

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
%Define some length registers
\newdimen\lengthAB

% Define two points
\def\pointA{\pgfpoint{0cm}{0cm}}
\pgfcoordinate{A}{\pointA}
\def\pointB{\pgfpoint{1.5cm}{2cm}}
\pgfcoordinate{B}{\pointB}

%The function accepts points, otherwise zero!
\pgfmathanglebetweenpoints{\pointA}{\pointB}
\edef\angleAB{\pgfmathresult}

%Altermundus taught me this
\makeatletter
\pgfpointdiff{\pointA}{\pointB}
    \pgf@xa=\pgf@x % no need to use a new dimen
    \pgf@ya=\pgf@y
\pgfmathparse{veclen(\pgf@xa,\pgf@ya)}
\makeatother
\pgfmathsetlength{\lengthAB}{\pgfmathresult}


% Test if they give the same result
\draw[ultra thick,red] (0,0) -- (B) ;
\draw (0,0) -- (\angleAB:\lengthAB);

\begin{scope}[shift={(\angleAB:\lengthAB)},rotate=\angleAB,sloped]
% Test if the origin is translated
\node[above] (a) at (0,0) {$(\pgfmathparse{\lengthAB/28.45274}\pgfmathresult \text{cm}, \angleAB^\circ )$};
%Test the orientation with a line that should be going down
\draw[thick] (a) -- ++(-90:2);
\end{scope}

\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容