TikZ 画直线的垂线

在平面几何中, 不时会需要画直线的垂线, 在 2020 年最新版的 TikZ 3.1.5b 中 P151 就提供了如何画垂线的样例, 包括过直线外一点和直线上某点. 这里精简了代码并加了注释, 顺便标了直角. 另外对于标记直角符号更多内容可以参考
https://www.qinxu.net/latex/2020/0620206,
英文还可以的建议直接阅读手册.

该样例分两个 tikzpicture 环境, 放在一个 文档中. 该代码只在 TeXlive 2020 中测试过. 效果如图, 代码在下.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc,angles}

\begin{document}

%% 过直线外一点作该直线的垂线
\begin{tikzpicture}
\coordinate[label=left:$A$] (a) at (0,0);
\coordinate[label=right:$B$] (b) at (2,2);
\coordinate[label=right:$C$] (c) at (0,3);

\draw (a) -- (b)
(c) -- ($(a)!(c)!(b)$) coordinate (d); %% ($(a)!(c)!(b)$) 是过 c 作 ab 的垂线时的垂足, 无论 c 的位置
\draw pic [draw] {right angle = a--d--c}; %% b--d--c 中间不能有空格, 需要 angles library
\end{tikzpicture}
%% 过直线上一点作该直线的垂线, 并标记直角
\begin{tikzpicture}
\coordinate[label=left:$A$] (a) at (0,0);
\coordinate[label=right:$B$] (b) at (4,3);
\coordinate[label=below:$D$] (d) at ($ (a)!.36!(b) $); %% ($ (a)!.36!(b) $) 是从 a 到 b 的 36% 的位置
\coordinate[label=above:$C$] (c) at ($ (d)!2.4cm!90:(b) $); %% ($ (d)!2.4cm!90:(b) $) 是线段 db 绕 d 旋转 90 度方向, 到 d 长度为 2.4cm 的位置

\draw (a) -- (b) -| cycle
(c) -- node [sloped,above] {2.4cm} (d)
pic [draw, angle radius = 3mm] {right angle = b--d--c};
\end{tikzpicture}

\end{document}

对于这个代码, 该解释的都在注释中, 若还有问题可以直接联系.