How to draw with TikZ a line perpendicular to a line from a point?

Given a point and line, how to draw a perpendicular line?

For instance, how do I draw the line segment AP following graph with TikZ, without hand-mathing the location of point P? Math gives me 3/8 and 13/16 in this case, but calculating it by hand is inelegant IMO and not always possible.

\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}


I tried googling and reading the PGF manual, but I wasn't intelligent enough to find any related content :( I think I missed some libraries or so.

With calc library (see section 13.5 Coordinate Calculations) is easy:

\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}


• Exactly what I am looking for! :D Commented Nov 9, 2018 at 12:07

Have you considered the tkz-euclide package? It has many tools to draw geometric forms. An example for your picture could look like the following:

\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}


The result:

• I never learned about this package :P I will take a look at it. Commented Nov 9, 2018 at 12:19
• Oh, and it has its documentation in French :-) Commented Nov 9, 2018 at 12:20