def distance_point_lineseg_squared(p, a, b):
    """Get the shortest distance from a point to a line segment.

	This can either be a perpendicular to a point on the line segment or the straight connection of p to one of the end points.

	@type p: Vector
	@param p: The point to compare to the line segment

	@type a: Vector
	@param a: The first point on the first line segment

	@type b: Vector
	@param b: The second point on the first line segment
	"""
    ap = p - a
    ab = b - a
    bp = p - b
    r = float(ap * ab) / ab.length_squared
    if r <= 0:
        return ap.length_squared
    if r >= 1:
        return bp.length_squared
    s = (a.y - p.y) * (b.x - a.x) - (a.x - p.x) * (b.y - a.y)
    return float(s * s) / ab.length_squared
