title: Trajectory intersection in python
body: <p>I'm detecting persons and vehicles using tensorflow and python. I calculate the trajectories and predict them using Kalman filter and I fit a line for predicting the trajectory.</p>
  
  <p>My problem is how would I find the intersection and time of collision between the two trajectories ?</p>
  
  <p>I tried line to line intersection but the fitted line is not always a two point lines, it's a polyline.
  Here is my attempt:</p>
  
  <pre><code> detections = tracker.update(np.array(z_box))
  
      for trk in detections[0]:
              trk = trk.astype(np.int32)
              helpers.draw_box_label(img, trk, trk[4])  # Draw the bounding boxes on the
              centerCoord = (((trk[1] +trk[3]) / 2), (trk[0] + trk[2]) / 2)
              point_lists[trk[4]].append(centerCoord)
              x = [i[0] for i in point_lists[trk[4]]]
              y = [i[1] for i in point_lists[trk[4]]]
              p = np.polyfit(x, y, deg=1)
              y = p[1] + p[0] * np.array(x)
              fitted = list(zip(x, y))
              cv2.polylines(img, np.int32([fitted]), False, color=(255, 0, 0))
              for other in detections[0]:
                  other = other.astype(np.int32)
                  if other[4] != trk[4]: # check for self ID
                      x2 = [i[0] for i in point_lists[other[4]]]
                      y2 = [i[1] for i in point_lists[other[4]]]
                      p2 = np.polyfit(x2, y2, deg=1)
                      y2 = p2[1] + p2[0] * np.array(x2)
                      other_fitted = list(zip(x2, y2))
                      if(line_intersection(fitted, other_fitted)):
                          print("intersection")
                      else:
                          print("not intersection")
  </code></pre>