contains method

bool contains (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

  _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;
}