Python Intersection function doesn't work when lines share just one common endpoint

211 Views Asked by At

enter image description here

I have these endpoints for function for line 1 (1,1) (4,1) line 2 (4,1) (8,1).

Output is empty because div becomes 0 , how to handle this case in this function so that they are not treated as parallel lines and i get the endpoint as the intersection point?

My python function for intersection

  def line_intersection (line1, line2): 
        line1 = eval(str(line1))
        line2 = eval(str(line2))
        
        x1,x2,x3,x4  = float(line1[0][0]),float(line1[1][0]),float(line2[0][0]),float(line2[1][0])
        y1,y2,y3,y4  = float(line1[0][1]),float(line1[1][1]),float(line2[0][1]),float(line2[1][1])
        


        xdiff = (x1- x2,  x3 - x4)
        ydiff = (y1 - y2, y3 - y4)

        def det(a, b):
            i = a[0] * b[1] 
            j =  a[1] * b[0]
            return i - j

        div = det(xdiff, ydiff)
        if div == 0:
            # print("lines do not intersect")
            return 0

        d = (det(*line1), det(*line2))
        x = det(d, xdiff) 
        x = x/ div
        y = det(d, ydiff) 
        y = y /div
        point = (x, y)
        

        x1, x2 = sorted([x1, x2])
        y1, y2 = sorted([y1, y2])
        if x < x1 or x > x2 or y < y1 or y > y2:
            # print("intersection point not within line segment")
            return 0

        x1, x2 = sorted([x1, x2])
        y1, y2 = sorted([y1, y2])
        if x < x1 or x > x2 or y < y1 or y > y2:
            # print("intersection point not within line segment")
            return 0

            return point

line1 = ((1, 1), (4, 1))
line2 = ((4, 1), (8, 1))
print(line_intersection(line1, line2))
1

There are 1 best solutions below

5
Tiago Vicente On

When div equals 0, the lines are considered to be parallel, and the function returns 0. If you want to handle the case where the lines are parallel and not return 0, you can add an if statement to check if div equals 0 and return a specific value or message indicating that the lines are parallel.

See if this helps:

def line_intersection(line1, line2): 
    line1 = eval(str(line1))
    line2 = eval(str(line2))

    x1,x2,x3,x4  = float(line1[0][0]),float(line1[1][0]),float(line2[0][0]),float(line2[1][0])
    y1,y2,y3,y4  = float(line1[0][1]),float(line1[1][1]),float(line2[0][1]),float(line2[1][1])

    # check if the lines are collinear
    if (y2 - y1) * (x4 - x3) == (y4 - y3) * (x2 - x1):
        # check if they have a common endpoint
        if (x1, y1) == (x3, y3) or (x1, y1) == (x4, y4) or (x2, y2) == (x3, y3) or (x2, y2) == (x4, y4):
            # return the common endpoint
            return (x1, y1) if (x1, y1) == (x3, y3) or (x1, y1) == (x4, y4) else (x2, y2)
        else:
            return None

    xdiff = (x1- x2,  x3 - x4)
    ydiff = (y1 - y2, y3 - y4)

    def det(a, b):
        i = a[0] * b[1] 
        j =  a[1] * b[0]
        return i - j

    div = det(xdiff, ydiff)
    if div == 0:
        return None

    d = (det(*line1), det(*line2))
    x = det(d, xdiff) 
    x = x/ div
    y = det(d, ydiff) 
    y = y /div
    point = (x, y)

    x1, x2 = sorted([x1, x2])
    y1, y2 = sorted([y1, y2])
    if x < x1 or x > x2 or y < y1 or y > y2:
        return None

    x1, x2 = sorted([x1, x2])
    y1, y2 = sorted([y1, y2])
    if x < x1 or x > x2 or y < y1 or y > y2:
        return None

    return point

line1 = ((1, 1), (4, 1))
line2 = ((4, 1), (8, 1))
print(line_intersection(line1, line2))

Let me know if I can give any further help