point.adhereToRect(r)

If the point lies outside the rectangle r, adjust the point so that it becomes the nearest point on the boundary of r.

r