nearestPoint method
Returns a point on the shape's boundary which is closest to the given
point
.
The point
must lie outside of the shape. If there are multiple nearest
points, any one can be returned.
This method will not modify point
. At the same time, the caller does
not get ownership of the returned object: they must treat it as an
immutable short-lived object.
Implementation
@override
Vector2 nearestPoint(Vector2 point) {
_tmpResult.setValues(
point.x.clamp(_left, _right),
point.y.clamp(_top, _bottom),
);
if (containsPoint(_tmpResult)) {
return _tmpResult;
}
_tmpCenter.setValues(
_tmpResult.x <= _left + _radius ? _left + _radius : _right - _radius,
_tmpResult.y <= _top + _radius ? _top + _radius : _bottom - _radius,
);
return _tmpResult
..setFrom(point)
..sub(_tmpCenter)
..length = _radius
..add(_tmpCenter);
}