Search code examples
pythonnumpyintersectioneuclidean-distanceline-intersection

Find the shortest distance between a point and line segments (not line)


I have set of line segments (not lines), (A1, B1), (A2, B2), (A3, B3), where A,B are ending points of the line segment. Each A and B has (x,y) coordinates.

QUESTION: I need to know the shortest distance between point O and line segments as shown in the shown figure implemented in line of codes. The code I can really understand is either pseudo-code or Python.

CODE: I tried to solve the problem with this code, unfortunately, it does not work properly.

def dist(A, B, O):
    A_ = complex(*A)
    B_ = complex(*B)
    O_= complex(*O)
    OA = O_ - A_
    OB = O_ - B_
    return min(OA, OB)
# coordinates are given
A1, B1 = [1, 8], [6,4]
A2, B2 = [3,1], [5,2]
A3, B3 = [2,3], [2, 1]
O = [2, 5]
A = [A1, A2, A3]
B = [B1, B2, B3]
print [ dist(i, j, O)  for i, j in zip(A, B)]

figure

Thanks in advance.


Solution

  • Here is the answer. This code belongs to Malcolm Kesson, the source is here. I provided it before with just link itself and it was deleted by the moderator. I assume that the reason for that is because of not providing the code (as an answer).

    import math
    
    def dot(v,w):
        x,y,z = v
        X,Y,Z = w
        return x*X + y*Y + z*Z
    
    def length(v):
        x,y,z = v
        return math.sqrt(x*x + y*y + z*z)
    
    def vector(b,e):
        x,y,z = b
        X,Y,Z = e
        return (X-x, Y-y, Z-z)
    
    def unit(v):
        x,y,z = v
        mag = length(v)
        return (x/mag, y/mag, z/mag)
    
    def distance(p0,p1):
        return length(vector(p0,p1))
    
    def scale(v,sc):
        x,y,z = v
        return (x * sc, y * sc, z * sc)
    
    def add(v,w):
        x,y,z = v
        X,Y,Z = w
        return (x+X, y+Y, z+Z)
    
    
    # Given a line with coordinates 'start' and 'end' and the
    # coordinates of a point 'pnt' the proc returns the shortest 
    # distance from pnt to the line and the coordinates of the 
    # nearest point on the line.
    #
    # 1  Convert the line segment to a vector ('line_vec').
    # 2  Create a vector connecting start to pnt ('pnt_vec').
    # 3  Find the length of the line vector ('line_len').
    # 4  Convert line_vec to a unit vector ('line_unitvec').
    # 5  Scale pnt_vec by line_len ('pnt_vec_scaled').
    # 6  Get the dot product of line_unitvec and pnt_vec_scaled ('t').
    # 7  Ensure t is in the range 0 to 1.
    # 8  Use t to get the nearest location on the line to the end
    #    of vector pnt_vec_scaled ('nearest').
    # 9  Calculate the distance from nearest to pnt_vec_scaled.
    # 10 Translate nearest back to the start/end line. 
    # Malcolm Kesson 16 Dec 2012
    
    def pnt2line(pnt, start, end):
        line_vec = vector(start, end)
        pnt_vec = vector(start, pnt)
        line_len = length(line_vec)
        line_unitvec = unit(line_vec)
        pnt_vec_scaled = scale(pnt_vec, 1.0/line_len)
        t = dot(line_unitvec, pnt_vec_scaled)    
        if t < 0.0:
            t = 0.0
        elif t > 1.0:
            t = 1.0
        nearest = scale(line_vec, t)
        dist = distance(nearest, pnt_vec)
        nearest = add(nearest, start)
        return (dist, nearest)