Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.04 KB | None | 0 0
  1. def calculate_intersections(wireA, wireB):
  2.     intersections = []
  3.     for i in range(len(wireA)):
  4.         for j in range(len(wireB)):
  5.             if i < len(wireA) - 1 and j < len(wireB) - 1:
  6.                 if abs(wireA[i][0]) - abs(wireA[i + 1][0]) == 0:
  7.                     # line A is going vertically
  8.                     # 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
  9.                     if abs(wireB[j][1]) - abs(wireB[j + 1][1]) == 0:
  10.                         # b line is going horizontally
  11.                         xcut = wireA[i][0]
  12.                         ycut = wireB[j][1]
  13.                         right = wireB[j][0] < wireB[j + 1][0]
  14.                         up = wireA[i][1] < wireA[i + 1][1]
  15.                         stepsB = abs(wireB[j][0] - wireA[i][0]) + wireB[j][2]
  16.                         stepsA = abs(wireA[i][1] - wireB[j][1]) + wireA[i][2]
  17.                         if right:
  18.                             # first point has a smaller x than second point
  19.                             if wireB[j][0] < xcut and wireB[j + 1][0] > xcut:
  20.                                 if up:
  21.                                     if wireA[i][1] < ycut and wireA[i + 1][1] > ycut:
  22.                                         intersections.append([xcut, ycut, stepsA, stepsB])
  23.                                 else:
  24.                                     if wireA[i][1] > ycut and wireA[i + 1][1] < ycut:
  25.                                         intersections.append([xcut, ycut, stepsA, stepsB])
  26.                         else:
  27.                             if wireB[j][0] > xcut and wireB[j + 1][0] < xcut:
  28.                                 if up:
  29.                                     if wireA[i][1] < ycut and wireA[i + 1][1] > ycut:
  30.                                         intersections.append([xcut, ycut, stepsA, stepsB])
  31.                                 else:
  32.                                     if wireA[i][1] > ycut and wireA[i + 1][1] < ycut:
  33.                                         intersections.append([xcut, ycut, stepsA, stepsB])
  34.                 elif abs(wireA[i][1]) - abs(wireA[i + 1][1]) == 0:
  35.                     # this line is going horizontally
  36.                     # 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
  37.                     if abs(wireB[j][0]) - abs(wireB[j + 1][0]) == 0:
  38.                         # b line is going vertically
  39.                         xcut = wireB[j][0]
  40.                         ycut = wireA[i][1]
  41.                         right = wireA[i][0] < wireA[i + 1][0]
  42.                         up = wireB[j][1] < wireB[j + 1][1]
  43.                         stepsB = abs(wireA[i][1] - wireB[j][1]) + wireB[j][2]
  44.                         stepsA = abs(wireA[i][0] - wireB[j][0]) + wireA[i][2]
  45.                         if up:
  46.                             # first point has a smaller x than second point
  47.                             if wireB[j][1] < ycut and wireB[j + 1][1] > ycut:
  48.                                 if right:
  49.                                     if wireA[i][0] < xcut and wireA[i + 1][0] > xcut:
  50.                                         intersections.append([xcut, ycut, stepsA, stepsB])
  51.                                 else:
  52.                                     if wireA[i][0] > xcut and wireA[i + 1][0] < xcut:
  53.                                         intersections.append([xcut, ycut, stepsA, stepsB])
  54.                         else:
  55.                             if wireB[j][1] > ycut and wireB[j + 1][1] < ycut:
  56.                                 if right:
  57.                                     if wireA[i][0] < xcut and wireA[i + 1][0] > xcut:
  58.                                         intersections.append([xcut, ycut, stepsA, stepsB])
  59.                                 else:
  60.                                     if wireA[i][0] > xcut and wireA[i + 1][0] < xcut:
  61.                                         intersections.append([xcut, ycut, stepsA, stepsB])
  62.     return intersections
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement