I asked "How can I tell if a point belongs to a certain line?" before and I found a suitable answer so thank you very much.
Now, I would like to know how to tell if a certain point is close to my line.
I asked "How can I tell if a point belongs to a certain line?" before and I found a suitable answer so thank you very much.
Now, I would like to know how to tell if a certain point is close to my line.
Calculate the point on your line that is closest to that point.
Assuming the line segment is a and b, and the point is p.
Which gives you 'amount', how far through the line segment you are between A and B (properly bounded).
Gives you point (nx,ny).
This will properly work beyond the end of the line segment, because it keeps 'amount' between 0 and 1.
If you don't want it a bounded line segment get rid of the bounds for amount. The rest of the code will still work, calculating positions beyond and before A and beyond B.
There was another question that claimed this question was a duplicate but, it's asking for a different thing hence my solution solves for the position of the point and then just solves the Euclidean distance (which actually solves both questions).
a.distanceSq(b) can also be done as vABxvABx + vAByvABy, since we already have those done.
@Alan Jackson's answer is almost perfect - but his first (and most up-voted) comment suggests that endpoints are not correctly handled. To ensure the point is on the segment, simply create a box where the segment is a diagonal, then check if the point is contained within. Here is the pseudo-code:
Given Line ab, comprised of points a and b, and Point p, in question:
You need to calculate the right angle distance to the line. Then you have to define what "close" is and test if it is within that distance.
The equation you want is:
How close is near?
Some geometry will give you the answer you need, you just need to be aware of the following steps.
Assuming your like is of the form y=mx+b, the shortest distance to your point will be the line perpendicular to your starting line (m1=-1/m), intersecting your point in question.
From there you calculate the distance between the intersection point and the point in question.
Google is your friend: Point-Line Distance (2-Dimensional). You can just use the equation at the bottom and there you go.
Basically, what you want to do it find the normal line — that is, a line perpendicular to your line — that intersects your point and the line, and then compute the distance along that line.