There is problem in it, it's calcuate more than two intersection point, but actually its has two, if you could draw the points two polygon would form. Please help me what is wrong in code ?
polygon1 = [(1,0),(3,0),(3,2),(1,2),(1,0)]
polygon2 = [(0,1),(2,1),(2,3),(0,3),(0,1)]
def linesIntersection(A, B, C, D):
x1 = A[0]
y1 = A[1]
x2 = B[0]
y2 = B[1]
x3 = C[0]
y3 = C[1]
x4 = D[0]
y4 = D[1]
denomenator = (((x1-x2)*(y3-y4)) - ((y1-y2)*(x3-x4)))
if denomenator != 0:
P1 = (((x1*y2 - y1*x2)*(x3-x4)) - ((x1-x2)*(x3*y4-y3*x4)))
Px = P1 / denomenator
P2 = (((x1*y2 - y1*x2)*(y3-y4)) - ((y1-y2)*(x3*y4-y3*x4)))
Py = P2 / denomenator
else:
return None
if Px == Py:
print "The Intersection points are: ", Px, Py
polygon1 = [(1,0),(3,0),(3,2),(1,2),(1,0)]
polygon2 = [(0,1),(2,1),(2,3),(0,3),(0,1)]
poly1 = len(polygon1)
poly2 = len(polygon1)
for i in range(poly1-1):
A = polygon1[i]
B = polygon1[i+1]
for j in range(poly2-1):
C = polygon2[j]
D = polygon2[j+1]
linesIntersection(A, B, C, D)