tikz 中两点之间的角度和距离?

tikz 中两点之间的角度和距离?

给定环境中的两个(A)(B)二维点tikzpicture,我需要在两个变量中分别收集线相(A)-(B)对于的角度vector(1,0)和其距离的一半,即\Aab\Dab解决了(看坐标 A、B:计算 |BA| 以及 +x 与 (BA) 之间的角度

\documentclass{article}
\usepackage{pgf,tikz}
\usetikzlibrary{calc}
\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}
    \clip (0,4) rectangle (7,-5);
    \coordinate (A) at (1,1);
    \coordinate (B) at (3,4);
    \getLengthAndAngle{A}{B}    
    \draw[help lines,gray] (0,-3) grid (5,5);
    \begin{scope}[blue, thick]  
    \draw (A) -- (B)--+(\mylength,0); 
    \draw[rotate around={-\myangle:(A)}] (A)--+(\mylength,0);
    \end{scope}
    \draw (B) circle (\mylength cm);
    \end{tikzpicture} 
\end{document}

答案1

这是一个完全基于的替代方法calc,即不使用额外的宏,并使用一个小助手允许您“导出”路径(和范围)之外的长度。

\documentclass{article}
\usepackage{pgf,tikz}
\usetikzlibrary{calc}
\begin{document}
    \begin{tikzpicture}[globalize/.code n args={2}{\xdef#2{#1}}]
    \clip (0,4) rectangle (7,-5);
    \coordinate (A) at (1,1);
    \coordinate (B) at (3,4);
    \draw[help lines,gray] (0,-3) grid (5,5);
    \begin{scope}[blue, thick]  
    \draw let \p1=($(B)-(A)$), \n1={veclen(\y1,\x1)}, \n2={veclen(\y1,\x1)}
     in [globalize={\n2}{\mylength}] (A) -- (B)--+(\n2,0)
     [rotate around={-\n1:(A)}] (A)--+(\n2,0);
    \end{scope}
    \draw (B) circle (\mylength);
    \end{tikzpicture} 
\end{document}

在此处输入图片描述

相关内容