I have the below end points of line segments.
Line segment 1:
A = (X1, Y1)
B = (X2, Y2)
Line segment 2:
C = (X3, Y3)
D = (X4, Y4)
I want to get the point of intersection of these two line segments.
I tried like this:
def intersectionfn(l1,l2):
x1,y1 = l1.src.x, l1.src.y
x2,y2 = l1.dst.x, l1.dst.y
x3,y3 = l2.src.x, l2.src.y
x4,y4 = l2.dst.x, l2.dst.y
try:
xnum = float(((x1*y2-y1*x2)*(x3-x4))-((x1-x2)*(x3*y4-y3*x4)))
xden = float(((x1-x2)*(y3-y4))-((y1-y2)*(x3-x4)))
#print xnum, xden
xcoor = float(xnum)/xden
#print xcoor
ynum = float((x1*y2-y1*x2)*(y3-y4) - (y1-y2)*(x3*y4-y3*x4))
yden = float((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4))
#print ynum, yden
ycoor = float(ynum)/yden
#print ycoor
return Point(xcoor,ycoor)
except:
print "No intersection.. 0/0 attempted"
But I am getting the output for infinitely long lines. I'm not aware of how to find it for line segments. If there is any algorithm/mathematical rule for it, please suggest it to me. I will implement it.