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))

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:
Let me know if I can give any further help