How to determine if a Point is within a certain distance of a line segment on a graph?

486 Views Asked by At

What I know:

There is a line segment from A to B. There is a third point C. I know...

  • Point As (x,y) values
  • Point Bs (x,y) values
  • Point Cs (x,y,) values

I can figure out if C is on the line segment. This is achievable by determining if the "Distance from A to B" === ("Distance from A to C" + "Distance from C to B")


However I want to be able to find out if Point C is "close" to the line segment, just not just "on the line segment" so it has room for "padding." i.e. "within n-number of points"

I figured the best way to do this is to construct a rectangle around the line segment, equal distance above and below. Then calculate if Point C is within the rectangle.

min

So you would:

  1. Construct a rectangle given two points on a line segment with a value for the padding above and below the line segment

  2. Check if Point C is within the rectangle - which will likely be rotated on the graph.

But how would you do each of these? especially considering the rectangle would be rotated? would you need to know the angle or rotation?

1

There are 1 best solutions below

0
On

Convenient recipe would be to express point $C$ location in coordinates tied to your line segment. That is how far along $AB$ direction and how far off the $AB$ line.

You can achieve this by projecting $AC$ vector onto $(AB,\perp AB)$ basis. First compute $C-A$ to get $v = (c_x - a_x, c_y - a_y)$. Then find the components along each axis using dot product by

$along = (v . e_1) / |e_1|$

$across = (v . e_2) / |e_2|$

Where $e_1 = (b_x - a_x, b_y - a_y)$ and $e_2 = (-b_y + a_y, b_x - a_x)$

Working like this you can conveniently express you problem with inequalities such as $along < |AB|$.

Wiki: dot product