我有一些 tikz 代码可以绘制带有标记顶点的三角形。
我想画一条从原点开始、与标记为 P 的点相交、长度等于标记为 A 的边的线。
我认为 calc 库就是我想要的,但我无法弄清楚如何将 A 的长度提供给它...
这是我目前的代码,我对我想要绘制的边缘命令的最佳猜测是:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections, calc, through, backgrounds}
\begin{document}
\begin{tikzpicture}[scale=3]
% layout some coordinates for a triangle
\coordinate (P) at ($(0,0) + (rand,rand)$);
\coordinate (Q) at ($(2,-2) + .5*(rand, rand)$);
\coordinate (R) at ($(-2, -2) + .5*(rand, rand)$);
%convienience for finding the centre
\coordinate (PQR) at ($(P)+(Q)+(R)$);
\coordinate (O) at ($1/3*(PQR)$);
%draw triangle
\draw[name path=A] (P) -- (Q);
\draw[name path=B] (Q) -- (R);
\draw[name path=C] (R) -- (P);
%labels
\node at (O) {$O$};
\node at (P) [above=2pt]{$P$};
\node at (Q) [right=2pt]{$Q$};
\node at (R) [left=2pt]{$R$};
\node at ($(P)!.5!(Q)$) [above right=1em]{$\mathbf{A}$};
%draw a path of length (A) from (O), passing through (P)
%\draw[name path=U] (O) -- ($(O)! !(B)$)
\end{tikzpicture}
\end{document}
答案1
使用tkz-euclide
,你可以使用这个:
\documentclass[tikz,border=10pt]{standalone}
\usepackage{tkz-euclide}
\begin{document}
\begin{tikzpicture}[scale=3]
% Coordinates for a triangle
\coordinate (P) at ($(0,0) + (rand,rand)$);
\coordinate (Q) at ($(2,-2) + .5*(rand, rand)$);
\coordinate (R) at ($(-2, -2) + .5*(rand, rand)$);
\coordinate (O) at (barycentric cs:P=1,Q=1,R=1) ;
% Draw triangle
\draw (P) -- (Q) node[midway,above,sloped] {dist $=A$} -- (R) -- cycle;
\tkzDrawPoints(P,Q,R,O)
% Labels
\node at (O) [left=2pt]{$O$};
\node at (P) [above right]{$P$};
\node at (Q) [right=2pt]{$Q$};
\node at (R) [left=2pt]{$R$};
% Calculate length of segment PQ and label as dPQ
\tkzCalcLength(P,Q)\tkzGetLength{dPQ}
% Create some point on a circle centred on O and with radius = PQ length
\tkzDefPointOnCircle[angle=90,center=O,radius=\dPQ pt]
\tkzGetPoint{I}
% Find intersection between line OP and previous circle
% Two points, but only one is useful
\tkzInterLC(O,P)(O,I) \tkzGetPoints{D}{E}
% Draw the segment from O, passing through P, the length of PQ
\draw[dashed,blue] (O) -- (E) node[pos=0.7,left] {dist$=A$};
\tkzDrawPoints(E)
\tkzLabelPoints[left](E)
\end{tikzpicture}
\end{document}
编辑
利用这个其他聪明的选项(除此之外veclen
我绝对不会使用),这里还有另一个更短的版本,用于tikz-euclide
计算线段长度和绘制线段的更简单实现。
\documentclass[tikz,border=10pt]{standalone}
\usepackage{tkz-euclide}
\begin{document}
\begin{tikzpicture}[scale=3]
% Coordinates for a triangle
\coordinate (P) at ($(0,0) + (rand,rand)$);
\coordinate (Q) at ($(2,-2) + .5*(rand, rand)$);
\coordinate (R) at ($(-2, -2) + .5*(rand, rand)$);
\coordinate (O) at (barycentric cs:P=1,Q=1,R=1) ;
% Draw triangle
\draw (P) -- (Q) node[midway,above,sloped] {dist $=A$} -- (R) -- cycle;
\tkzDrawPoints(P,Q,R,O)
% Labels
\node at (O) [left=2pt]{$O$};
\node at (P) [above right]{$P$};
\node at (Q) [right=2pt]{$Q$};
\node at (R) [left=2pt]{$R$};
% Calculate length of segment PQ
\tkzCalcLength(P,Q)\tkzGetLength{dPQ}
% Draw the segment from O, passing through P, the length of PQ
\draw[orange,thick,dashed] (O)--++ ($(O)!\dPQ pt!(P)-(O)$) coordinate[at end] (E);
\tkzDrawPoints(E)
\tkzLabelPoints[left](E)
\end{tikzpicture}
\end{document}
答案2
(\x,\y)
您可以使用计算“向量”的长度sqrt(\x1*\x1+\y1*\y1)
。然后使用构造
($(A)!<len>!(B)-(A)$)
创建一个从 (A) 到 (B) – (A) 方向的矢量,其长度为<len>
:
这里,蓝线的端点为 (A) = (1,0) 和 (B) = (3,1)。红线从 (C) = (1,1) 到 (D) = (2,2) 绘制,线长从 (A) 到 (B),使用代码
\draw[red,thick] let\p1=($(B)-(A)$) in (C)--++($(C)!sqrt(\x1*\x1+\y1*\y1)!(D)-(C)$);
该let
命令\p1
将从 (A) 分配给 (B) 的“向量”,并自动将其 x 和 y 分量分配给\x1
和\y1
。
如果愿意,您可以定义一个\thru
接受 5 个参数的命令,其中一个是可选的:
\newcommand{\thru}[5][]{\draw[#1] let\p1=($#5-#4$) in #2--++($#2!sqrt(\x1*\x1+\y1*\y1)!#3-#2$);}
可选参数可以是任何tikz
你想要的属性。第一个必需参数是你的起点,第二个是终点,第三个和第四个是你想要的长度的线段的端点。
因此,\thru[green,thick]{(2,1)}{(3,2)}{(A)}{(B)}
将从 (2,1) 到 (3,2) 绘制一条粗绿线,其长度为线段 (A)--(B)。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\thru}[5][]{\draw[#1] let\p1=($#5-#4$) in #2--++($#2!sqrt(\x1*\x1+\y1*\y1)!#3-#2$);}
\begin{document}
\begin{tikzpicture}
\draw[help lines] (0,0) grid (4,3);
\coordinate(A) at (1,0);
\coordinate(B) at (3,1);
\coordinate(C) at (1,1);
\coordinate(D) at (2,2);
\draw[blue,thick] (A)--(B);
\draw[red,thick] let\p1=($(B)-(A)$) in (C)--++($(C)!sqrt(\x1*\x1+\y1*\y1)!(D)-(C)$);
\thru[green,thick]{(2,1)}{(3,2)}{(A)}{(B)}
\end{tikzpicture}
\end{document}