![tikz 中两点之间的角度和距离?](https://linux22.com/image/385917/tikz%20%E4%B8%AD%E4%B8%A4%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E8%A7%92%E5%BA%A6%E5%92%8C%E8%B7%9D%E7%A6%BB%EF%BC%9F.png)
给定环境中的两个(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}