Distance between line and point in homogeneous coordinates?

2.4k Views Asked by At

I can't seem to find any definitive answer to this question.

Assuming I have a 2D line in homogeneous coordinates defined by $$l = (a, b, c)^T$$ and a point in 2D space, $$x = (x, y)^T,$$ how do I find the perpendicular distance between the two.

I know it has something to do with the dot product, but I can't remember exactly how it works. It would be nice if you could walk me through why your answer is correct.

2

There are 2 best solutions below

1
On

$x^Tl=0$ is line fomula where $(l_1,l_2)^T$ is the normal and $l_3$ is the y offset at $x=0$ of the line.

For computing the distance, we shift the whole graph by the offset and compute the scalar product of the normalized normal of the line with the shifted point.

The normalized normal of the line is $n=\frac{\begin{pmatrix} l_1 \\ l_2 \end{pmatrix}}{\sqrt{l_1^2+l_2^2}}$. Finally $d = \frac{\left(\begin{pmatrix} x\\ y\end{pmatrix}-\begin{pmatrix} 0\\ -l_3/l_2\end{pmatrix}\right)^T\begin{pmatrix} l_1\\ l_2\end{pmatrix}}{\sqrt{l_1^2+l_2^2}}$ which results to the result given previously. The sign depends on which side the point x from the line l, therefore you can take the absolute value.

0
On

Following from this, here is a python numpy solution

import numpy as np


def distance_points_to_lines_homo(points_homo_nx3: np.array, lines_homo_nx3: np.array):
    # see https://homepages.inf.ed.ac.uk/rbf/CVonline/LOCAL_COPIES/BEARDSLEY/node2.html
    normalized_lines_nx3 = lines_homo_nx3 / np.linalg.norm(lines_homo_nx3[:, :2], axis=1)[:, np.newaxis]
    normalized_points_nx3 = points_homo_nx3 / points_homo_nx3[:, 2][:, np.newaxis]
    # the following is the same as `distances = np.diag(normalized_lines_nx3 @ normalized_points_nx3.T)`, but efficient
    distances = np.sum(normalized_lines_nx3 * normalized_points_nx3, axis=1)
    abs_distances = np.abs(distances)
    return abs_distances