def closest_point_on_line(a, b, p):
  """
  Returns the closest point to *p* which is on the line through the points
  *a* and *b*. #ZeroDivisionError is raised if *a* and *b* are the same.
  """

  ap = a - p
  ab = b - a
  ab2 = ab.x ** 2 + ab.y ** 2 + ab.z ** 2
  ab_ap = (ap.x * ab.x) + (ap.y * ab.y) + (ap.z * ab.z)
  t = ab_ap / ab2
  return a - ab * t