views:

412

answers:

5

I need a function to find the shortest distance between two line segments. A line segment is defined by two endpoints. So for example one of my line segments (AB) would be defined by the two points A (x1,y1) and B (x2,y2) and the other (CD) would be defined by the two points C (x1,y1) and D (x2,y2).

Feel free to write the solution in any language you want and I can translate it into javascript. Please keep in mind my geometry skills are pretty rusty. I have already seen http://stochastix.wordpress.com/2008/12/28/distance-between-two-lines/ and I am not sure how to translate this into a function. Thank you so much for help.

+1  A: 

Is this in 2 dimensions? If so, the answer is simply the shortest of the distance between point A and line CD, B and CD, C and AB or D and AB. So it's a fairly simple "distance between point and line" calculation (if the distances are all the same, then the lines are parallel).

This site explains the algorithm for distance between a point and a line pretty well.

It's slightly more tricky in the 3 dimensions because the lines are not necessarily in the same plane, but that doesn't seem to be the case here?

Dean Harding
But if the segments intersect, the minimum distance between each endpoint and its opposite segment could still be nonzero....or have I misunderstood the problem?
Jim Lewis
Oh yeah, I missed that particular case :) If they intersect then obviously the minimum distance is 0. Paul Bourke to the rescue again: http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
Dean Harding
Yes codeka this is in 2D. I was thinking there was something more elegant than having to repeat distance check four times. But this makes sense and I accept this answer.
Frank
+1  A: 

Distance between Lines and Segments with their Closest Point of Approach

TheMachineCharmer
That's for the 3D case, I don't think Frank needs this in 3D...
Dean Harding
I have seen this link as well but this is for 3D. I do not think it applies here?
Frank
2D is just a special case of 3D...
Lasse V. Karlsen
(n)D is just a special case of (n + 1)D.
Jon Purdy
A: 

Taken from this example, which also comes with a simple explanation of why it works as well as VB code (that does more than you need, so I've simplified as I translated to Python -- note: I have translated, but not tested, so a typo might have slipped by...):

def segments_distance(x11, y11, x12, y12, x21, y21, x22, y22):
  """ distance between two segments in the plane:
      one segment is (x11, y11) to (x12, y12)
      the other is   (x21, y21) to (x22, y22)
  """
  if segments_intersect(x11, y11, x12, y12, x21, y21, y22, y22): return 0
  # try each of the 4 vertices w/the other segment
  distances = []
  distances.append(point_segment_distance(x11, y11, x21, y21, x22, y22))
  distances.append(point_segment_distance(x12, y12, x21, y21, x22, y22))
  distances.append(point_segment_distance(x21, y21, x11, y11, x12, y12))
  distances.append(point_segment_distance(x22, y22, x11, y11, x12, y12))
  return min(distances)

def segments_intersect((x11, y11, x12, y12, x21, y21, x22, y22):
  """ whether two segments in the plane intersect:
      one segment is (x11, y11) to (x12, y12)
      the other is   (x21, y21) to (x22, y22)
  """
  dx1 = x12 - x11
  dy1 = y12 - y11
  dx2 = x22 - x21
  dy2 = y22 - y21
  delta = dx2 * dy1 - dy2 * dx1
  if delta == 0: return False  # parallel segments
  s = (dx1 * (y21 - y11) + dy1 * (x11 - x21)) / delta
  t = (dx2 * (y11 - y21) + dy2 * (x21 - x11)) / (-delta)
  return (0 <= s <= 1) and (0 <= t <= 1)

import math
def point_segment_distance(px, py, x1, y1, x2, y2):
  dx = x2 - x1
  dy = y2 - y1
  if dx == dy == 0:  # the segment's just a point
    return math.hypot(px - x1, py - y1)

  # Calculate the t that minimizes the distance.
  t = ((px - x1) * dx + (py - y1) * dy) / (dx * dx + dy * dy)

  # See if this represents one of the segment's
  # end points or a point in the middle.
  if t < 0:
    dx = px - x1
    dy = py - y1
  elif t > 1:
    dx = px - x2
    dy = py - y2
  else:
    near_x = x1 + t * dx
    near_y = y1 + t * dy
    dx = px - near_x
    dy = py - near_y

  return math.hypot(dx, dy)
Alex Martelli
This is very good example thank you.
Frank
A: 

All 2D lines unless they are parallel will eventually meet. Learn what is being taught so you understand it rather than trying to cheat this particular q.

mP
But not all 2d, non-parallel line *segments* intersect.
Wallacoloo
I am referring to a segment not a line.
Frank
A: 

For calculating the minimum distance between 2 2D line segments it is true that you have to perform 4 perpendicular distance from endpoint to other line checks successively using each of the 4 endpoints. However, if you find that the perpendicular line drawn out does not intersect the line segment in any of the 4 cases then you have to perform 4 additional endpoint to endpoint distance checks to find the shortest distance. Whether there is a more elegent solution to this I do not know, though I hope there is! lol

Stephen