Distance From a Line

TikZ figures for a derivation from dot product

Background

A secondary school student posted a coordinate geometry question on a Discord homework help server. A helper replied to that question with an illustration of the Perpendicular Distance Formula

$$ d((x_1,y_1), L) = \frac{\lvert Ax_1 + By_1 + C \rvert}{\sqrt{A^2 + B^2}}, $$

where $L$ is the line $Ax + By + C = 0$ without proof.

Goal

To provide an illustrated derivation of this formula.

Recall

A basic property of the dot product: the “algebraic definition” is equivalent to the “geometric definition”.

$$ \vec{a} \cdot \vec{b} = a_1 b_1 + a_2 b_2 = \lVert \vec{a} \rVert \lVert \vec{b} \rVert \cos \theta, $$

where $\theta$ is the angle between $\vec{a}$ and $\vec{b}$.

Illustration

tikz figures for the distance formula

Graph generated by dvisvgm from DVI

Two figures for the derivation of the Formula

In fact, $\lvert C \rvert$ ($C$ comes from the equation of $L$) is the product of

  1. $d(O, L)$: (perpendicular) distance between the origin and $L$, and
  2. $\color{red}{\lVert \vec{\mathbf{n}} \rVert}$: the magnitude (or length) of the normal vector $\color{red}{\vec{\mathbf{n}}}$.

Source code

  • amsmath has to be loaded for multi-line equations with aligned. I don’t know (and I’ve no time to find out) why align doesn’t work inside a TikZ node.

  • On Discord, mycolors are 60% white.

  • The code for the second figure had exceeded the limits of 2,000 characters, so I cut down the title and some coloring in the title so that it passed the TeXit bot.

  • I first hard-coded the coordinates of the \node[draw], but on Discord, it overlapped with the straight line $L$. To solve this, I put it

    • at (P0 |- P): below $P_0$ (or $N_0$) and on the left of $P$
    • anchor=north so that the top centeral point of the box is set to the aforementioned point.
  • In the first figure, thanks to my LaTeX plugin in VS code, I could quickly input ($ (P)!(N1)!(P0) $) with one click and a few modifications. This denotes the (perpendicular) projection of $N_1$ on line $\overline{P_0P}$.

  • I’m not sure if the middle argument <ratio> for convex combination of two points (A) and (B): ($ (A)!<ratio>!(B) $) in TikZ calc library accepts values outside of the closed interval [0,1]. I learnt from TeX.SE the following trick.

    \path (A) -- (B)
        coordinate[pos=-0.5] (start)
        coordinate[pos=1.5] (end);
    \draw (start) -- (end);
    

    \path (A) -- (B); alone won’t draw the line segment bewteen (A) and (B).

\documentclass[preview]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\colorlet{myblue}{blue}
\colorlet{myred}{red}
\begin{figure}
    \centering
    \caption{A normal vector of the line $Ax + By + C = 0$ is $\color{myred}{\vec{\mathbf{n}} = (A,B)}$}
    \begin{tikzpicture}
        % P* on line L
        \coordinate[label=below left:${(x_0,y_0)}$] (P0) at (1,1);
        \coordinate[label=below:${(x,y)}$] (P) at (4,0);
        % N* on normal vector (A,B)
        \coordinate (N1) at (-1, 4);
        \coordinate (N0) at ($ (P)!(N1)!(P0) $);
        
        % draw point labels
        \draw (P) node {$\times$};
        \draw (P0) node {$\times$};
        % draw line and vector P->P_0
        \draw[-stealth,thick,myblue] (P0) -- (P)
            node[midway,sloped,above=0.5cm,anchor=south,myblue]{$\vec{\mathbf{r}}(x,y) = (x-x_0,y-y_0)$}
            coordinate[pos=-1](start)
            coordinate[pos=1.5](end);
        \draw[dashed] (start) -- (end);
        % draw normal vector n = N0->N1
        \draw[-stealth,thick,myred] (N0) -- (N1)
            node[midway,left,myred] {$\vec{\mathbf{n}} = (A,B)$};
        \node[draw, anchor=north] at (P0 |- P) {
            $\begin{aligned}
                \color{myred}{\vec{\mathbf{n}}} \cdot \color{myblue}{\vec{\mathbf{r}}} &= 0 \\
                \color{myred}{A}\color{myblue}{(x-x_0)} + \color{myred}{B}\color{myblue}{(y-y_0)} &= 0 \\
                Ax + By \underbrace{-(Ax_0 + By_0)}_{C} &= 0
            \end{aligned}$
        };
    \end{tikzpicture}
\end{figure}

\begin{figure}
    \centering
    \caption{Distance of a point $(x_1,y_1)$ from the line $Ax + By + C = 0$ is ${\color{gray}{\vec{\mathbf{d}}}} \cdot {\color{myred}{\vec{\mathbf{n}}}}/{\color{myred}{\lVert \vec{\mathbf{n}} \rVert}}$}
    \begin{tikzpicture}
        % P* on line L
        \coordinate (P0) at (1,1);
        \coordinate[label=below:${(x,y)}$] (P) at (4,0);
        % N* on normal vector (A,B)
        \coordinate (N1) at (-1, 4);
        \coordinate[label=below left:${(x_0,y_0)}$] (N0) at ($ (P)!(N1)!(P0) $);
        % P1 = (x_1,y_1)
        \path (N0) -- (N1)
            coordinate[pos=2,label=above right:${(x_1,y_1)}$] (P1) at (-1, 4);
        
        % draw point labels
        \draw (P) node {$\times$};
        \draw (P1) node {$\times$};
        \draw (N0) node {$\times$};
        % draw line and vector P->P_0
        \draw[-stealth,thick,myblue] (N0) -- (P)
            node[midway,sloped,above=0.5cm,anchor=south,myblue]{$\vec{\mathbf{r}}(x,y) = (x-x_0,y-y_0)$}
            coordinate[pos=-0.3](start)
            coordinate[pos=1.5](end);
        \draw[dashed] (start) -- (end);
        % draw vector N0->P1
        \draw[-stealth,thick,dashed,gray] (N0) -- (P1)
            node[pos=0.7,anchor=west,gray]{${\mathbf{d}} = (x_1-x_0,y_1-y_0)$};
        % draw normal vector n = N0->N1
        \draw[-stealth,thick,myred] (N0) -- (N1)
            node[midway,left,myred] {$\vec{\mathbf{n}} = (A,B)$};
        \node[draw, anchor=north] at (N0 |- P) {
            $\begin{aligned}
                d((x_1,y_1), L) &= \frac{{\color{gray}{\vec{\mathbf{d}}}} \cdot {\color{myred}{\vec{\mathbf{n}}}}}{\color{myred}{\lVert \vec{\mathbf{n}} \rVert}} \\
                &= \frac{\color{myred}{A}\color{gray}{(x_1-x_0)} + \color{myred}{B}\color{gray}{(y_1-y_0)}}{\color{myred}{\sqrt{A^2 + B^2}}} \\
                &= \frac{Ax_1 + By_1 - (Ax_0 + By_0)}{\color{myred}{\sqrt{A^2 + B^2}}} \\
                &= \frac{Ax_1 + By_1 + C}{\color{myred}{\sqrt{A^2 + B^2}}}
            \end{aligned}$
        };
    \end{tikzpicture}
\end{figure}
\end{document}
LaTeX 

No comment

Your email address will not be published. Required fields are marked *.