给定两个点(定义,例如使用节点),我想计算它们之间的距离。
- 是否有一些内置功能可以
tikz
实现这一点? - 如果不是,那么如何使用数学引擎来实现?
我想到的应用是绘制一个以第二点为中心(a)
并经过第二点的圆弧(b)
,其中只有(a)
和(b)
是已知的。
答案1
正如 wh1t3 所评论的,有一个through
库甚至有命令circle through
。这是手册中的示例:\usetikzlibrary{through}
在序言中添加该行后,
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\node (a) at (2,1.5) {$a$};
\node [draw] at (1,1) [circle through={(a)}] {$c$};
\end{tikzpicture}
calc
您可以使用几乎同样方便的库来执行此操作(我在输入答案时 Ignasi 对此进行了评论)。您还可以将其用于其他目的:稍微修改示例并在序言中使用,您可以使用以下命令\usetikzlibrary{calc}
获取向量长度:veclen
\begin{tikzpicture}
\coordinate [label=left:$A$] (A) at (0,0);
\coordinate [label=right:$B$] (B) at (2,2);
\draw[red,line width=1mm] let \p1 = ($(B)-(A)$) in (A) -- ++(45:({veclen(\x1,\y1)}););
\draw (A) -- (B);
\draw[blue] (A) let \p1 = ($(B)-(A)$) in -- ++({veclen(\x1,\y1)},0) arc (0:45:({veclen(\x1,\y1)}););
\end{tikzpicture}
这将给予
答案2
使用TikZ
,您就可以用 percusse 得到答案,使用 pgfmath 是相同的方法,但您需要确定由两个点形成的向量的坐标 vx、vy,然后将\pgfmathparse{veclen(vx,vy)}
结果放在 中\pgfmathresult
。
就我个人而言,我对 TikZ 的语法抱有一定幻想。我对该语法不太满意let \p1 \n1
,我更喜欢在绘制对象之前计算长度。此外,在某些情况下,结果不是很好,我也使用 fp 来计算长度。Lua 也是一种可能性。也可以使用fpu
带有 的库TikZ
。
\documentclass[11pt]{scrartcl}
\usepackage{tikz,fp}
\usetikzlibrary{calc}
\makeatletter
\def\calcLength(#1,#2)#3{%
\pgfpointdiff{\pgfpointanchor{#1}{center}}%
{\pgfpointanchor{#2}{center}}%
\pgf@xa=\pgf@x%
\pgf@ya=\pgf@y%
\FPeval\@temp@a{\pgfmath@tonumber{\pgf@xa}}%
\FPeval\@temp@b{\pgfmath@tonumber{\pgf@ya}}%
\FPeval\@temp@sum{(\@temp@a*\@temp@a+\@temp@b*\@temp@b)}%
\FProot{\FPMathLen}{\@temp@sum}{2}%
\FPround\FPMathLen\FPMathLen5\relax
\global\expandafter\edef\csname #3\endcsname{\FPMathLen}
}
\makeatother
\begin{document}
5cm = 5*28.45274 pt =142.2637pt
\begin{tikzpicture}
\coordinate (A) at (1,2);
\coordinate (B) at (4,6);
\calcLength(A,B){mylen}
% \draw (A) circle (\mylen pt); % pt is important here
\end{tikzpicture}
With calclength the length of AB is : \mylen
\begin{tikzpicture}
\coordinate (A) at (1,2);
\coordinate (B) at (4,6);
\path (A) let \p1 = ($ (B) - (A) $), \n1 = {veclen(\x1,\y1)}
in -- (B) node[draw] {With veclen the length is :\n1};
\end{tikzpicture}
\end{document}
要获得弧,代码是
\calcLength(A,B){mylen}
\draw[red,line width=1mm] (A) -- ++(45:\mylen pt);
\draw[blue] (A) -- ++(\mylen pt,0) arc (0:45:\mylen pt);
对我来说,它更具可读性,但这只是个人品味的问题
答案3
TikZ 库math
看起来非常直观:)
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{math}
\begin{document}
\begin{tikzpicture}
%Two points (A) and (B)
\coordinate (A) at (0,0);
\coordinate (B) at (2,2);
%--Computing the distance between (A) and (B)
%Creating a math coordinate
\tikzmath{coordinate \C;
%Storing coordinates difference
\C = (B)-(A);
%Computing the length of C = (Cx,Cy) from its components Cx and Cy
%Note the length \distAB is in points (pt)
\distAB = sqrt((\Cx)^2+(\Cy)^2);
}
%--Drawing
%line A -- B
\draw (A) node [above] {A} -- (B) node[above] {B} node[midway]{\distAB pt};
%Circle with center in (A) and radius \distAB points
\draw (A) circle (\distAB pt);
\end{tikzpicture}
\end{document}
笔记:您无需手动计算范数\distAB = sqrt((\Cx)^2+(\Cy)^2);
,而是可以在 TikZ 库中看到math
函数veclen(Vx,Vy)
。我不鼓励使用它,因为精度太低了。
附加溶液(单位:cm)
令人困惑的是,TikZ 点默认接受 的长度cm
(例如,\coordinate (A) at (2,2)
平均值(2cm,2cm)
),但 的坐标计算tikzmath
是在 中执行并输出的pt
。出于这个原因,我对上面的代码进行了微小的修改,以便所有长度都以厘米为单位一致表示。(\convertto
宏的鸣谢请访问菲利普·古特)
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{math}
\makeatletter
\def\convertto#1#2{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1}
\makeatother
\begin{document}
\begin{tikzpicture}
%Two points (A) and (B)
%Note: Default TikZ coordinates are centimeters
% but inner computations are performed in pt
\coordinate (A) at (0,0);
\coordinate (B) at (2,2);
%--Computing the distance between (A) and (B)
%Creating a math coordinate
\tikzmath{coordinate \C;
%Storing coordinates difference
\C = (B)-(A);
%Computing the length of C = (Cx,Cy) from its components Cx and Cy
%Note the length \distAB is in points (pt)
\distAB = sqrt((\Cx)^2+(\Cy)^2);
%Convert back to centimeters
\distAB = \convertto{cm}{\distAB pt};
}
%--Drawing
%line A -- B
\draw (A) node [above] {A} -- (B) node[above] {B} node[midway]{\distAB cm};
%Circle with center in (A) and radius \distAB cm
%Note that we need not specify (\distAB cm) because
%the standard length unit in TikZ drawing is centimeter
\draw (A) circle (\distAB);
\end{tikzpicture}
\end{document}