给定一个点和一条线,如何画一条垂直线?
例如,如何使用 TikZ 绘制线段AP
跟随图,而无需手动计算点的位置P
?在这种情况下,数学给出了 3/8 和 13/16,但手动计算在我看来不够优雅,而且并不总是可行的。
\begin{tikzpicture}
\coordinate[label=below left:\(A\)] (A) at (0,0);
\coordinate[label=below right:\(B\)] (B) at (2,0);
\coordinate[label=above left:\(C\)] (C) at (0,1);
\coordinate[label=above right:\(P\)] (P) at (0.375,0.8125);
% ^^ there must be a way better than this ^^
\draw (A) -- (B) -- (C) -- (A) -- (P);
\end{tikzpicture}
我尝试用谷歌搜索并阅读 PGF 手册,但我不够聪明,找不到任何相关内容 :( 我想我错过了一些库之类的。
答案1
使用calc
库(参见第 13.5 节坐标计算)很容易:
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate[label=below left:\(A\)] (A) at (0,0);
\coordinate[label=below right:\(B\)] (B) at (2,0);
\coordinate[label=above left:\(C\)] (C) at (0,1);
% \coordinate[label=above right:\(P\)] (P) at (0.375,0.8125);
% ^^ there must be a way better than this ^^
\draw (A) -- (B) -- (C) -- cycle;
\draw (A) -- ($(C)!(A)!(B)$) node[above right] {\(P\)};
\end{tikzpicture}
\end{document}
答案2
您考虑过这个tkz-euclide
软件包吗?它有许多工具可以绘制几何图形。您的图片示例可能如下所示:
\documentclass{article}
\usepackage{tkz-euclide}
\usetkzobj{all}
\begin{document}
\begin{tikzpicture}[scale=4]
\tkzDefPoint(0,0){A}
\tkzDefPoint(2,0){B}
\tkzDefPoint(0,1){C}
\tkzDefPointBy[projection=onto B--C](A) \tkzGetPoint{P}
\tkzDrawSegments(A,B B,C C,A A,P)
\tkzLabelPoints[above right](P)
\tkzLabelPoints[below right](B)
\tkzLabelPoints[below left](A)
\tkzLabelPoints[above left](C)
\end{tikzpicture}
\end{document}
结果: