# Peripendicular distance from a line segment

I have a line given by $Ax + By + C= 0$, and a point $x0,y0$. From that point $x0,y0$ in the direction of the line up to distance $d$, I want to find the perpendicular distance of the points from this line segment.

In the figure, below I wish to calculate this only for $x1,y1$ and $x4,y4$. The points $x2,y2$ and $x3,y3$ should be excluded as they lie above the red line depicted. Please check figure below:

• I just can't understand this question, and the diagram helps nothing: what does "finding points that are less than distance" mean at all? You don't seem to be asking how to calculate the distance from $\;(x,y)\;$ to that line, but then what are you asking? And what is $\;d_2\;$ in the diagram, and how does it get defined? – Timbuc Jul 1 '15 at 10:03
• @Timbuc yes, what are saying makes more sense. I tend to do that. – Abhishek Bhatia Jul 1 '15 at 10:05
• Do you actually want to have a line given by $Ax+By+C=0$ and a point $x,y$ on that line? If so, it would make things much clearer if you wrote it that way. Also, it's a bit confusing to use the same variables $x,y$ to denote general points on the line (or in the plane) and also to denote a particular fixed point. It would be clearer if your special point were named something else, perhaps $(x_0,y_0)$. Note that when you use a list of coordinates such as this to name a point, it's conventional to enclose the list in parentheses. – David K Jul 1 '15 at 10:53
• If your problem statement is correct, then the constraint $Ax_0+By_0+C=0$ must hold. – Yves Daoust Jul 1 '15 at 12:50
• The problem statement is incomplete as nothing tells you the orientation of the line. – Yves Daoust Jul 1 '15 at 12:54

Translate all points so that $(x_0,y_0)$ becomes $(0,0)$.

The unit vector $(a,b):=(A,B)/\sqrt{A^2+B^2}$ is perpendicular to the black line, so that a vector $(x,y)$ decomposes as $d_\perp=(a,b)\cdot(x,y)$ and $d_\parallel=(b,-a)\cdot(x,y)$.

The answer is given by $|d_\perp|$ if $0\le d_\parallel\le d$.

• I think these formulas are equivalent (in the end) to what I wrote up, but in a much neater and more concise notation. – David K Jul 1 '15 at 16:47
• One doubt: if "(black) line" means the black line in the OP's diagram, I think OP wanted $|d_\perp|$ if $0\le d_\parallel\le d$. (The "$0 \le$" part isn't completely clear, but now that you point it out I suppose it may be part of OP's desired outcome.) – David K Jul 1 '15 at 16:50
• Yep, I fixed the typo, thanks. As the problem statement is a little ambiguous, I mixed algebraic and absolute distances. Of course, our formulas are the same, but normalization helps. One can also say "WLOG (by a change of coordinates), the line is $x=0$ and the point $(0,0)$" :-) – Yves Daoust Jul 1 '15 at 17:04
• Normalization indeed helps! (That's why I gave this answer its first upvote; it's a significant improvement.) Aside from the obvious benefits, your normalization constant eliminates that annoying $\varepsilon$ that I had to spend so many words explaining. – David K Jul 1 '15 at 17:11
• Subtract $x_0$ and $y_0$ from all coordinates. Products are $ax+by$ and $bx-ay$. – Yves Daoust Jul 2 '15 at 11:10

The distance $d(P,D)$ of your $P$ with coordinates $(x_P,y_P)$ to the line $D \equiv Ax+By+X=0$ is given by the formula $$d(P,D) = \frac{\vert Ax_P + By_P+C \vert}{\sqrt{A^2+B^2}}$$

• I mentioned this in my first comment: I doubt highly this is what the OP meant to ask. OTOH, I've no idea what he actually meant to ask... – Timbuc Jul 1 '15 at 10:21
• The line $Ax+By+C=0$ is constrained upto a distance d from point x,y in it's direction. Thus, in addition to the above it should exclude points which lie beyond d as well. – Abhishek Bhatia Jul 1 '15 at 10:25
• Does this make sense? – Abhishek Bhatia Jul 1 '15 at 10:25
• Not sure I understand. Can you precise the way the line is constrainted? What are the circles in your drawing? – mathcounterexamples.net Jul 1 '15 at 10:33
• Circles are simply points. – Abhishek Bhatia Jul 1 '15 at 10:38

As best I can understand the question, what you really want to do is related to this figure:

Here we see your point $(x_0, y_0)$, the line $L$ given by $Ax+By+C=0$, and the $x$- and $y$-coordinate axes with respect to which those $x$ and $y$ coordinates are defined. (I am assuming you meant for the line $L$ to pass through the point $(x_0, y_0)$.)

The figure also shows a line $M$ through $(x_0, y_0)$ perpendicular to the line $L$. Together, these two lines define a transformed coordinate system that is translated and rotated with respect to the $x,y$ coordinate system. An arbitrary point $(x,y)$ somewhere in the plane, as shown in the figure, has coordinates $(x',y')$ in the transformed system, where $x'$ is measured parallel to $L$ and $y'$ is measured perpendicular to $L$.

Your red line appears near the upper right corner of the figure, perpendicular to the line $L$. The distance from the line $M$ to the red line is $d$. The equation of the red line (in the transformed coordinate system) would be $x' = d.$

It is my understanding that for such an arbitrary point $(x,y)$, with transformed coordinates $(x',y')$, you want to know first of all whether $x' \leq d$. If $x' \leq d$, you then want to know the value of $|y'|$, which is the perpendicular distance from $(x,y)$ to the line $L$ given by $Ax+By+C=0$. If $x' > d$, then $x'$ is on the "wrong" side of the red line and you are not interested in its distance from the line $L$.

The transformed coordinates are found by the equations

$$x' = \frac{\varepsilon (B(x - x_0) - A(y - y_0))}{\sqrt{A^2 + B^2}}$$ $$y' = \frac{\varepsilon (A(x - x_0) + B(y - y_0))}{\sqrt{A^2 + B^2}}$$

where $\varepsilon$ is a constant and $\varepsilon=1$ or $\varepsilon=-1$ depending on which of those two values produces the desired sign of $x'$. For a point at coordinates $(x,y)$, compute $x'$ according to the equation above; then, if $x'\leq d$, compute $|y'|$.

The reason for $\varepsilon$ is so that the red line will be in the desired direction from $(x_0,y_0)$: one value of $\varepsilon$ will cause the line $x'=d$ to be to the right of $(x_0,y_0)$ and the other value of $\varepsilon$ will cause the line to be to the left. Which value of $\varepsilon$ puts the line on which side depends on the signs of $A$ and $B$. You can figure out which value of $\varepsilon$ to use by trial and error: set a value of $\varepsilon$, choose a point $(x,y)$ that is obviously on the "wrong" side of the red line, and compute that point's $x'$ coordinate. If $x' > d$, you have set $\varepsilon$ to the correct value; otherwise you should reverse the sign of $\varepsilon$.

Note that because taking the absolute value cancels the sign of $y'$, and because we assumed that $Ax_0 + By_0 + C = 0,$ the formula for $|y'|$ using the equation above is equal to the usual formula for finding the distance of a point from a line given by $Ax + By + C = 0$, $$|y'| = \frac{|Ax + By + C|}{\sqrt{A^2 + B^2}}.$$

• You explained the question way better than me! I still confused over e – Abhishek Bhatia Jul 1 '15 at 13:01
• You can wait a while longer to see if any other helpful answers are posted, but don't forget to accept the answer you like best at the end. (It doesn't have to be mine!) Otherwise your question will be hang out in the archives "forever" in the "unanswered" state, as if it's still waiting for a satisfactory resolution. – David K Jul 1 '15 at 13:15
• @DavidK: Is the drawing done in tikz (or pgfplots)? – user170039 Jul 1 '15 at 15:40
• @user170039 I drew the figure in OpenOffice Draw. – David K Jul 1 '15 at 16:42