2
$\begingroup$

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

$\endgroup$
  • 1
    $\begingroup$ 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? $\endgroup$ – Timbuc Jul 1 '15 at 10:03
  • $\begingroup$ @Timbuc yes, what are saying makes more sense. I tend to do that. $\endgroup$ – Abhishek Bhatia Jul 1 '15 at 10:05
  • 1
    $\begingroup$ 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. $\endgroup$ – David K Jul 1 '15 at 10:53
  • 1
    $\begingroup$ If your problem statement is correct, then the constraint $Ax_0+By_0+C=0$ must hold. $\endgroup$ – Yves Daoust Jul 1 '15 at 12:50
  • 1
    $\begingroup$ The problem statement is incomplete as nothing tells you the orientation of the line. $\endgroup$ – Yves Daoust Jul 1 '15 at 12:54
3
$\begingroup$

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

| cite | improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ I think these formulas are equivalent (in the end) to what I wrote up, but in a much neater and more concise notation. $\endgroup$ – David K Jul 1 '15 at 16:47
  • 1
    $\begingroup$ 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.) $\endgroup$ – David K Jul 1 '15 at 16:50
  • 1
    $\begingroup$ 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)$" :-) $\endgroup$ – Yves Daoust Jul 1 '15 at 17:04
  • 1
    $\begingroup$ 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. $\endgroup$ – David K Jul 1 '15 at 17:11
  • 1
    $\begingroup$ Subtract $x_0$ and $y_0$ from all coordinates. Products are $ax+by$ and $bx-ay$. $\endgroup$ – Yves Daoust Jul 2 '15 at 11:10
2
$\begingroup$

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}}$$

| cite | improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ 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... $\endgroup$ – Timbuc Jul 1 '15 at 10:21
  • $\begingroup$ 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. $\endgroup$ – Abhishek Bhatia Jul 1 '15 at 10:25
  • $\begingroup$ Does this make sense? $\endgroup$ – Abhishek Bhatia Jul 1 '15 at 10:25
  • 1
    $\begingroup$ Not sure I understand. Can you precise the way the line is constrainted? What are the circles in your drawing? $\endgroup$ – mathcounterexamples.net Jul 1 '15 at 10:33
  • $\begingroup$ Circles are simply points. $\endgroup$ – Abhishek Bhatia Jul 1 '15 at 10:38
2
$\begingroup$

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

coordinate system

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}}.$$

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ You explained the question way better than me! I still confused over e $\endgroup$ – Abhishek Bhatia Jul 1 '15 at 13:01
  • $\begingroup$ 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. $\endgroup$ – David K Jul 1 '15 at 13:15
  • $\begingroup$ @DavidK: Is the drawing done in tikz (or pgfplots)? $\endgroup$ – user170039 Jul 1 '15 at 15:40
  • $\begingroup$ @user170039 I drew the figure in OpenOffice Draw. $\endgroup$ – David K Jul 1 '15 at 16:42

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.