0

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. enter image description here

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

3

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}

enter image description here

  • Exactly what I am looking for! :D – L. F. Nov 9 '18 at 12:07
1

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:

enter image description here

  • I never learned about this package :P I will take a look at it. – L. F. Nov 9 '18 at 12:19
  • Oh, and it has its documentation in French :-) – Sergei Golovan Nov 9 '18 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.