contains method Null safety

bool contains(
  1. Offset point
)

Whether the point specified by the given offset (which is assumed to be relative to the origin) lies inside the rounded rectangle.

This method may allocate (and cache) a copy of the object with normalized radii the first time it is called on a particular RRect instance. When using this method, prefer to reuse existing RRects rather than recreating the object each time.

Implementation

bool contains(Offset point) {
  if (point.dx < left || point.dx >= right || point.dy < top || point.dy >= bottom) {
    return false;
  } // outside bounding box

  final RRect scaled = scaleRadii();

  double x;
  double y;
  double radiusX;
  double radiusY;
  // check whether point is in one of the rounded corner areas
  // x, y -> translate to ellipse center
  if (point.dx < left + scaled.tlRadiusX &&
      point.dy < top + scaled.tlRadiusY) {
    x = point.dx - left - scaled.tlRadiusX;
    y = point.dy - top - scaled.tlRadiusY;
    radiusX = scaled.tlRadiusX;
    radiusY = scaled.tlRadiusY;
  } else if (point.dx > right - scaled.trRadiusX &&
             point.dy < top + scaled.trRadiusY) {
    x = point.dx - right + scaled.trRadiusX;
    y = point.dy - top - scaled.trRadiusY;
    radiusX = scaled.trRadiusX;
    radiusY = scaled.trRadiusY;
  } else if (point.dx > right - scaled.brRadiusX &&
             point.dy > bottom - scaled.brRadiusY) {
    x = point.dx - right + scaled.brRadiusX;
    y = point.dy - bottom + scaled.brRadiusY;
    radiusX = scaled.brRadiusX;
    radiusY = scaled.brRadiusY;
  } else if (point.dx < left + scaled.blRadiusX &&
             point.dy > bottom - scaled.blRadiusY) {
    x = point.dx - left - scaled.blRadiusX;
    y = point.dy - bottom + scaled.blRadiusY;
    radiusX = scaled.blRadiusX;
    radiusY = scaled.blRadiusY;
  } else {
    return true; // inside and not within the rounded corner area
  }

  x = x / radiusX;
  y = y / radiusY;
  // check if the point is outside the unit circle
  if (x * x + y * y > 1.0) {
    return false;
  }
  return true;
}