def _find_closest_point_on_leg(p1, p2, p0):
 if np.all((p2 == p1)):
  d = np.sum(((p0 - p1) ** 2))
  return (d, p1)
 d21 = (p2 - p1)
 d01 = (p0 - p1)
 proj = (np.dot(d01, d21) / np.dot(d21, d21))
 if (proj < 0):
  proj = 0
 if (proj > 1):
  proj = 1
 pc = (p1 + (proj * d21))
 d = np.sum(((pc - p0) ** 2))
 return (d, pc)

def _find_closest_point_on_leg ( p1 , p2 , p0 ) : if np . all ( ( p2 == p1 ) ) : d = np . sum ( ( ( p0 - p1 ) ** 2 ) ) return ( d , p1 ) d21 = ( p2 - p1 ) d01 = ( p0 - p1 ) proj = ( np . dot ( d01 , d21 ) / np . dot ( d21 , d21 ) ) if ( proj < 0 ) : proj = 0 if ( proj > 1 ) : proj = 1 pc = ( p1 + ( proj * d21 ) ) d = np . sum ( ( ( pc - p0 ) ** 2 ) ) return ( d , pc )
find closest point to p0 on line segment connecting p1 and p2 .