Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def calculate_intersections(wireA, wireB):
- intersections = []
- for i in range(len(wireA)):
- for j in range(len(wireB)):
- if i < len(wireA) - 1 and j < len(wireB) - 1:
- if abs(wireA[i][0]) - abs(wireA[i + 1][0]) == 0:
- # line A is going vertically
- # if line A is vertical we need to check to see if the coordinates of the b line is horizontal and has coordinates that cut the a lines y position
- if abs(wireB[j][1]) - abs(wireB[j + 1][1]) == 0:
- # b line is going horizontally
- xcut = wireA[i][0]
- ycut = wireB[j][1]
- right = wireB[j][0] < wireB[j + 1][0]
- up = wireA[i][1] < wireA[i + 1][1]
- stepsB = abs(wireB[j][0] - wireA[i][0]) + wireB[j][2]
- stepsA = abs(wireA[i][1] - wireB[j][1]) + wireA[i][2]
- if right:
- # first point has a smaller x than second point
- if wireB[j][0] < xcut and wireB[j + 1][0] > xcut:
- if up:
- if wireA[i][1] < ycut and wireA[i + 1][1] > ycut:
- intersections.append([xcut, ycut, stepsA, stepsB])
- else:
- if wireA[i][1] > ycut and wireA[i + 1][1] < ycut:
- intersections.append([xcut, ycut, stepsA, stepsB])
- else:
- if wireB[j][0] > xcut and wireB[j + 1][0] < xcut:
- if up:
- if wireA[i][1] < ycut and wireA[i + 1][1] > ycut:
- intersections.append([xcut, ycut, stepsA, stepsB])
- else:
- if wireA[i][1] > ycut and wireA[i + 1][1] < ycut:
- intersections.append([xcut, ycut, stepsA, stepsB])
- elif abs(wireA[i][1]) - abs(wireA[i + 1][1]) == 0:
- # this line is going horizontally
- # if a line is vertical we need to check to see if the coordinates of the b line is horizontal and has coordinates that cut the a lines x position
- if abs(wireB[j][0]) - abs(wireB[j + 1][0]) == 0:
- # b line is going vertically
- xcut = wireB[j][0]
- ycut = wireA[i][1]
- right = wireA[i][0] < wireA[i + 1][0]
- up = wireB[j][1] < wireB[j + 1][1]
- stepsB = abs(wireA[i][1] - wireB[j][1]) + wireB[j][2]
- stepsA = abs(wireA[i][0] - wireB[j][0]) + wireA[i][2]
- if up:
- # first point has a smaller x than second point
- if wireB[j][1] < ycut and wireB[j + 1][1] > ycut:
- if right:
- if wireA[i][0] < xcut and wireA[i + 1][0] > xcut:
- intersections.append([xcut, ycut, stepsA, stepsB])
- else:
- if wireA[i][0] > xcut and wireA[i + 1][0] < xcut:
- intersections.append([xcut, ycut, stepsA, stepsB])
- else:
- if wireB[j][1] > ycut and wireB[j + 1][1] < ycut:
- if right:
- if wireA[i][0] < xcut and wireA[i + 1][0] > xcut:
- intersections.append([xcut, ycut, stepsA, stepsB])
- else:
- if wireA[i][0] > xcut and wireA[i + 1][0] < xcut:
- intersections.append([xcut, ycut, stepsA, stepsB])
- return intersections
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement