For all intents and purposes this concept is an asked and answered question on this site. However, my question about this topic is much more to do with the general vector approach to solving this question. Not to mention, I could not quite follow the explanations here and here. This should be a rather trivial problem to solve, but it's driving me nuts.

Let $l = [a,b,1]$ be a normal vector defining a line such that $ax + by + 1 = 0$.

Let $p = [x_1,y_1,1]$ be a point on the same plane ($z = 1$).

I want to find the point $q = [x_2, y_2, 1]$ that minimizes the distance between $p$ and $q$. More formally:

$\underset{q} argmin ||p-q||_2$ subject to $l \cdot q = 0$.

I know that $q \cdot (p-q) = 0$ because the shortest line is perpendicular to the line $l$. I also know that $l \cdot q = 0$ because the point $q$ falls on the line $l$. What I cannot seem to grasp is how to find the components $x_2$ and $y_2$ of $q$.

Can someone please help me out?

Thanks in advance!

  • $\begingroup$ Please, try to improve the question by uniquely defining what is $p,q,l$ (lines? planes in $\Bbb R^3$? points in $\Bbb R^3$? lines in $\Bbb R^2$?) and what exactly you are asking. A point $q$ minimizing the distance between $p$ and $q$ is $p=q$, by the way. $\endgroup$ Dec 17 '15 at 22:55
  • $\begingroup$ @PeterFranek: "A point $q$ minimizing the distance between $p$ and $q$ is $p=q$" is not true given the constraint that $q$ must fall on the line unless $p$ also falls on the line, which is not guaranteed. As to the first part of your comment, it's all in the question. $l$ parametrizes a line. $p$ and $q$ are points on the same plane as the line. This could be $\mathbb{R}^n$--it doesn't matter because everything lies on the same plane $\endgroup$
    – marcman
    Dec 17 '15 at 23:08

To me your problem looks like this:

The line (l) and the point (p) are one the same plane (z=1), so this is a dimension-2 problem, first of all. We don't really need to worry about the z, in fact it can take any value. More to this: any plane can be defined with a line and a point, so even if you don't say that they are one the same plane, we can always find a plane that contains both.

Then, you need to reformulate your question. Finding a "closest point" on the line $l$ to a certain point $p$ is just finding the "distance" from the point $p$ to the line $l$. So the point $q$ you are looking for is just the orthogonal projection of $p$ onto $l$. So the right question should be: how could you find the image of $p$ projected orthogonally onto $l$? (there are oblique projections too, which are more complexe)

This is basic algebra (not even linear algebra). Visually: you have a line and a point, all you need to do is draw a perpendicular line (pq) onto (l). You know the "slope" of the new line (pq) using the fact that it's orthogonal to (l), you know one point of it (p), then you have the equation of (pq). So $q$ is just the intersection of the 2 lines, and now you have their equations. Solve a system of linear equations and you have the point $q$.

You looked into the wrong direction (with the optimization) for a simple problem. But don't worry, it happens all the time. My advice to you is trying "diffuse thinking" if you get stuck, don't get yourself locked in 1 direction. Asking the wrong question make you fail at the 1st step.


You are working in a three-dimensional space so to define a line you need two equations. In your case:

$l:\begin{cases} ax+by+1=0\\ z=1 \end{cases}$

Let's rewrite the equations of $l$ in a more convenient way. A point of $l$ is any solution of the system of equations, for instance, assuming $a\neq0$, $(-1/a,0,1)$.

A direction vector of $l$ can be found as follows:

$$\vec{v}_l=\begin{vmatrix} i&j&k\\ a&b&0\\ 0&0&1 \end{vmatrix}=(b,-a,0)$$

Then we can write any point of $l$ as $q=(-1/a,0,1)+t(b,-a,0)=(-1/a+tb,-ta,1)$ for some $t\in\mathbb{R}$.

Then, as you said, the point $q$ in $l$ that minimizes the distance between $p=(x_1,x_2,1)$ and $l$ is the point that satisfies the condition $\vec{pq}\cdot \vec{v}_l=0$. More precisely,


The only thing you need to do now is compute the dot product of $\vec{pq}$ and $\vec{v}_l$, equalize to $0$ and solve for $t$ ( remember $a$, $b$, $x_1$ and $x_2$ are given).

A different approach would be the following: consider a line $r$ in the plane $z=1$ that passes through $p$ and that is perpendicular to $l$. Then the point $q$ you're looking for is the intersection of $r$ and $l$.

Hope this helps!


In general you can also find a plane, call it $\pi$, perpendicular to $l$ that passes through point $p$. Then, the intersection $\pi\cap l$ is the point that minimizes the distance.


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