![绘制与给定线垂直的特定长度的线段](https://linux22.com/image/309718/%E7%BB%98%E5%88%B6%E4%B8%8E%E7%BB%99%E5%AE%9A%E7%BA%BF%E5%9E%82%E7%9B%B4%E7%9A%84%E7%89%B9%E5%AE%9A%E9%95%BF%E5%BA%A6%E7%9A%84%E7%BA%BF%E6%AE%B5.png)
我有一个典型的几何结构,我想用 来绘制它TikZ
。以下代码给出了一条线段PQ
。我如何在垂直于并穿过 的TikZ
线上绘制一个点,该点位于线段下方的单位处?我认为我必须在序言中使用它才能做到这一点。(我想将此点标记为 R 并绘制直角三角形。)如果我想用 来表示三角形是直角三角形,我必须在序言中使用它吗?PQ
P
2\sqrt{2}
PQ
\usetikzlibrary{calc}
QPR
\tkzMarkRightAngle(Q,P,R);
\usepackage{tkz-euclide}
\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning,intersections,quotes}
\begin{document}
\begin{tikzpicture}
\draw[yellow, line width=0.1pt] (-1.75,-1.75) grid[xstep=0.5, ystep=0.5] (2.75,1.75);
\draw[draw=gray!30,latex-latex] (0,1.75) +(0,0.25cm) node[above right] {$y$} -- (0,-1.75) -- +(0,-0.25cm);
\draw[draw=gray!30,latex-latex] (-1.75,0) +(-0.25cm,0) -- (2.75,0) -- +(0.25cm,0) node[below right] {$x$};
\node[outer sep=0pt,circle, fill,inner sep=1.5pt,label={[fill=white]left:$P$}] (P) at (-1,-1) {};
\node[outer sep=0pt,circle, fill,inner sep=1.5pt, label={[fill=white]right:$Q$}] (Q) at (2,1) {};
\draw[green!20!white] (P) -- (Q);
\end{tikzpicture}
\end{document}
答案1
检查代码中的解释性注释:
\documentclass{amsart}
\usepackage{tikz}
%% you need the following 2 lines to use \tkzMarkRightAngle
%\usepackage{tkz-euclide}
%\usetkzobj{all}
\usetikzlibrary{shapes,positioning,intersections,quotes,calc}
\begin{document}
\begin{tikzpicture}
\draw[yellow, line width=0.1pt] (-1.75,-5) grid[xstep=0.5, ystep=0.5] (2.75,1.75);
\draw[draw=gray!30,latex-latex] (0,1.75) +(0,0.25cm) node[above right] {$y$} -- (0,-1.75) -- +(0,-0.25cm);
\draw[draw=gray!30,latex-latex] (-1.75,0) +(-0.25cm,0) -- (2.75,0) -- +(0.25cm,0) node[below right] {$x$};
\node[outer sep=0pt,circle, fill,inner sep=1.5pt,label={[fill=white]left:$P$}] (P) at (-1,-1) {};
\node[outer sep=0pt,circle, fill,inner sep=1.5pt, label={[fill=white]right:$Q$}] (Q) at (2,1) {};
\draw[green!20!white] (P) -- (Q);
%% the perpendicular
\pgfmathparse{2*sqrt(5)}
\node[outer sep=0pt,circle, fill,inner sep=1.5pt,label={[fill=white]left:$R$}] (R) at ($ (P)!\pgfmathresult cm! -90:(Q) $) {};
\draw[green!20!white] (P) -- (R) -- (Q);
%% right angle with tkz-euclide
%\coordinate (p) at (P);
%\tkzMarkRightAngle[color=green!20!white](Q,p,R)
%\fill (p) circle (2.1pt); %% to make the dot above right angle again.
\coordinate (a) at ($ (P)!5mm! -45:(Q) $);
\draw[green!20!white] (a) -- ($(P)!(a)!(Q)$);
\draw[green!20!white] (a) -- ($(P)!(a)!(R)$);
\end{tikzpicture}
\end{document}
答案2
为了回答您的问题,tikz 几乎通过 pgfmathparse 运行所有内容,它看起来更像 C 代码:2 * sqrt(5),顺便说一下,在默认比例下超过 4 厘米。($(P)!1!90:(Q)$)表示从(P)开始,从到(Q)的方向以 90 度(逆时针)移动 1 倍距离到(Q)。
\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning,intersections,quotes,calc}
\begin{document}
\begin{tikzpicture}
\draw[yellow, line width=0.1pt] (-1.75,-1.75) grid[xstep=0.5, ystep=0.5] (2.75,1.75);
\draw[draw=gray!30,latex-latex] (0,1.75) +(0,0.25cm) node[above right] {$y$} -- (0,-1.75) -- +(0,-0.25cm);
\draw[draw=gray!30,latex-latex] (-1.75,0) +(-0.25cm,0) -- (2.75,0) -- +(0.25cm,0) node[below right] {$x$};
\node[outer sep=0pt,circle, fill,inner sep=1.5pt,label={[fill=white]left:$P$}] (P) at (-1,-1) {};
\node[outer sep=0pt,circle, fill,inner sep=1.5pt, label={[fill=white]right:$Q$}] (Q) at (2,1) {};
\draw[green!20!white] (P) -- (Q);
\coordinate (R) at ($(P)!2cm*sqrt(5)!-90:(Q)$);
\node[outer sep=0pt,circle, fill,inner sep=1.5pt, label={[fill=white]right:$R$}] at(R) {};
\end{tikzpicture}
\end{document}
答案3
实际上,您不必使用\usepackage{tkz-euclide}
。 它只是为您提供了\tkzMarkRightAngle(Q,P,R)
比纯 更简单的构造tikz
,但这也可以相对轻松地使用 only 实现,tikz
就像@HarishKumar 所做的那样。 此外,使用style
s 作为轴和点可以使代码更简洁。
\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}\small
\begin{tikzpicture}[outer sep=0pt,p/.style={circle, fill,inner sep=1.5pt},ax/.style={draw=gray!50,latex-latex}]
\draw[yellow,line width=0.1pt] (-2.75cm,-1.75cm) grid[xstep=0.5, ystep=0.5] (2.75cm,1.75cm);
\draw[ax](0,1.75cm) +(0,0.25cm) node[above] {$y$} -- (0,-1.75cm) -- +(0,-0.25cm);
\draw[ax](-2.75cm,0) +(-0.25cm,0) -- (2.75cm,0) -- +(0.25cm,0) node[right] {$x$};
\coordinate[p,label={[fill=white]below:$P$}] (P) at (-1cm,-1cm);
\coordinate[p,label={[fill=white]right:$Q$}] (Q) at (2cm,1cm);
\coordinate[p,label={[fill=white]left :$R$}] (R) at ($(P)!{2cm*sqrt(2)}!90:(Q)$);
\draw (P)--(R)--(Q)--(P)--cycle;
\coordinate (a) at ($(P)!4mm!45:(Q)$);
\draw ($(P)!(a)!(Q)$) -- (a) -- ($(P)!(a)!(R)$);
\end{tikzpicture}
\end{document}
答案4
对于那些有兴趣的人来说,一种方法(其中包括)MetaPost。
点 R 使得 PR 垂直于 PQ 且位于2\sqrt{2}
PQ 下方距 P 单位处,由以下指令给出(u
这里为单位长度cm
):
R = P + 2u*sqrt2*unitvector(Q-P) rotated -90;
anglebetween
MetaPost 的宏元乐趣如果参数设置为 2 ,格式允许在两个相交线段之间轻松绘制直角标记P--Q
(P--R
第三个参数是标签,这里是一个空字符串)。如果anglemethod
此参数的默认值为 1,则标记将为圆弧。标记的所需长度应作为另一个参数的参数给出anglelength
(默认为 20 pt)。
anglemethod := 2; anglelength := 2mm;
draw anglebetween(P--Q, P--R, "");
看Metafun 手册,第 279 页,了解详细介绍。
\documentclass[border=2mm]{standalone}
\usepackage{luamplib}
\mplibsetformat{metafun}
\mplibtextextlabel{enable}
\begin{document}
\begin{mplibcode}
% Axes parameters
u := cm; % Unit length
xmin := -1.75u; xstep := .5u; xmax := 2.75u;
ymin := -5u; ystep := xstep; ymax := 1.75u;
% Triangle summits
pair P, Q, R; P = u*(-1, -1); Q = u*(2, 1);
R = P + 2u*sqrt2*unitvector(Q-P) rotated -90;
beginfig(1);
% Grid
drawoptions(withcolor yellow);
for i = ceiling(xmin/xstep) upto floor(xmax/xstep):
draw (i*xstep, ymin) -- (i*xstep, ymax);
endfor
for j = ceiling(ymin/ystep) upto floor(ymax/ystep):
draw (xmin, j*ystep) -- (xmax, j*ystep);
endfor
% Axes
drawoptions(withcolor .8white);
drawarrow (xmin, 0) -- (xmax, 0);
drawarrow (0, ymin) -- (0, ymax);
% Triangle
drawoptions(withcolor green);
path triangle; triangle = P--Q--R--cycle; draw triangle;
% Right-angle mark of length 2 mm (and no label)
anglemethod := 2; anglelength := 2mm;
draw anglebetween(P--Q, P--R, "");
% Labels
drawoptions();
label.bot("$x$", (xmax, 0)); label.lft("$y$", (0, ymax));
dotlabel.lft("$P$", P); dotlabel.rt("$Q$", Q); dotlabel.bot("$R$", R);
endfig;
\end{mplibcode}
\end{document}
使用 LuaLaTeX 排版。输出: