tags:

views:

981

answers:

6

Given two 2D line segments, A and B, how do I calculate the length of the shortest 2D line segment, C, which connects A and B?

+3  A: 

This page has information you may be looking for.

strager
This is in 3d I think. It's not relevant to his question!
Alterlife
My head asplode!
spoulson
an answer for 3d will work for 2d, 2d just a special case for 3d where z always == 0. so in the sudo code at the bottom z(i) == z(j) == 0
David Waters
+1  A: 

Gernot Hoffmann paper (algorithm and Pascal code):

http://www.fho-emden.de/~hoffmann/xsegdist03072004.pdf

Nicolai
This is in 3d , It's not relevant to his question
Alterlife
+1  A: 

Quick tip: if you want to compare distances based on points, it's not necessary to do the square roots.

E.g. to see if P-to-Q is a smaller distance than Q-to-R, just check (pseudocode):

square(P.x-Q.x) + square(P.y-Q.y) < square(Q.x-R.x) + square(Q.y-R.y)
joel.neely
A: 

This page has a nice short description for finding the shortest distance between two lines, although @strager's link includes some code (in Fortran!)

Ian Hopkinson
That's 3D, and it refers to lines, not line segments. Not relevant here.
Jason S
The original question did not specify 2D.
Ian Hopkinson
OK. Apologies. Suggest whoever downvoted this remove their downvote. (I did not.)
Jason S
No problem - as a side-effect I've discovered how to view the edit history of a post!
Ian Hopkinson
+4  A: 

Consider your two line segments A and B to be represented by two points each:

line A represented by A1(x,y), A2(x,y)

Line B represented by B1(x,y) B2(x,y)

First check if the two lines intersect using this algorithm.

If they do intersect, then the distance between the two lines is zero, and the line segment joining them is the intersection point.

If they do not intersect, Use this method: http://local.wasp.uwa.edu.au/~pbourke/geometry/pointline/ to calculate the shortest distance between:

  1. point A1 and line B
  2. Point A2 and line B
  3. Point B1 and line A
  4. Point B2 and line A

The shortest of those four line segments is your answer.

Alterlife
Additionally: first check for A and B crossing each other (A1 + vector (A1->A2) * a = B1 + vector (B1->B2) * b with a and b real numbers != 0). Second check for A and B being parallel (i.e. vector (A1->A2) * a = vector (B1->B2) with being a real number != 0).
bothie
I don't belive this answer is correct. for one as per above if the lines cross the distance shortest between lines is zero. yet the shortest distance from any end point to the other line > 0.
David Waters
You're both right: it's necessary to check for intersection. However, I think it's unnecessary to check if the lines are parallel.
Alterlife
I think the intersection check needed is whether the line *segments* intersect, not the extended lines (which usually will). But it's not clear how to determine that ("this algorithm" isn't given?), even after checking the link for the point-to-line(-segment) determination.
Rob Parker
A: 

Afterlife's said, "First check if the two lines intersect using this algorithm," but he didn't indicate what algorithm he meant. Obviously, it's the intersection of the line segments not the extended lines which matters; any non-parallel line segments (excluding coincident endpoints which don't define a line) will intersect, but the distance between the line segments would not necessarily be zero. So I assume he meant "line segments" rather than "lines" there.

The link Afterlife gave is a very elegant approach to finding the closest point on a line (or line segment, or ray) to another arbitrary point. This works for finding the distance from each endpoint to the other line segment (constraining the calculated parameter u to be no less than 0 for a line segment or ray and to be no more than 1 for a line segment), but it doesn't handle the possiblity that an interior point on one line segment is closer than either endpoint because they actually intersect, thus the extra check about intersection is required.

As for the algorithm for determining line-segment intersection, one approach would be to find the intersection of the extended lines (if parallel then you're done), and then determine whether that point is within both line segments, such as by taking the dot-product of the vectors from the intersection point, T, to the two endpoints:

((Tx - A1x) * (Tx - A2x)) + ((Ty - A1y) * (Ty - A2y))

If this is negative (or "zero") then T is between A1 and A2 (or at one endpoint). Check similarly for the other line segment. If either was greater than "zero" then the line segments do not intersect. Of course, this depends on finding the intersection of the extended lines first, which may require expressing each line as an equation and solving the system by Gaussian reduction (etc).

But there may be a more direct way without having to solve for the intersection point, by taking the cross-product of the vectors (B1-A1) and (B2-A1) and the cross product of the vectors (B1-A2) and (B2-A2). If these cross-products are in the same direction, then A1 and A2 are on the same side of line B; if they are in opposite directions, then they are on opposite sides of line B (and if 0, then one or both are on line B). Similarly check the cross-products of vectors (A1-B1) and (A2-B1) and of (A1-B2) and (A2-B2). If any of these cross-products is "zero", or if the endpoints of both line segments fall on opposite sides of the other line, then the line segments themselves must intersect, otherwise they do not intersect.

Of course, you need a handy formula for computing a cross-product of two vectors from their coordinates. Or if you could determine the angles (being positive or negative), you wouldn't need the actual cross-product, since it's the direction of the angles between the vectors which we actually care about (or the sine of the angle, really). But I think the formula for cross-product (in 2-D) is simply:

Cross(V1,V2) = (V1x * V2y) - (V2x * V1y)

This is the z-axis component of the 3-D cross-product vector (where the x and y components must be zero, because the initial vectors are in the plane z=0), so you can simply look at the sign (or "zero").

So, you could use one of these two methods to check for line-segment intersection in the algorithm Afterlife describes (referencing the link).

Rob Parker