intersect method

  1. @override
List<Vector2> intersect(
  1. CircleComponent circle,
  2. PolygonComponent polygon, {
  3. Rect? overlappingRect,
})
override

Implementation

@override
List<Vector2> intersect(
  CircleComponent circle,
  PolygonComponent polygon, {
  Rect? overlappingRect,
}) {
  final intersectionPoints = <Vector2>[];
  final center = circle.absoluteCenter;
  final radius = circle.scaledRadius;
  // The bounding box of a rotated or unevenly scaled circle is larger than
  // the circle, so the edges only have to be within the bounds of the circle
  // itself.
  final circleBounds = Rect.fromCircle(
    center: center.toOffset(),
    radius: radius + _circleBoundsEpsilon,
  );
  final possibleVertices = polygon.possibleIntersectionVertices(
    overlappingRect?.intersect(circleBounds) ?? circleBounds,
  );
  for (final line in possibleVertices) {
    for (final intersection in line.circleIntersections(center, radius)) {
      if (!intersectionPoints.contains(intersection)) {
        intersectionPoints.add(intersection);
      }
    }
  }
  if (intersectionPoints.isEmpty && (circle.isSolid || polygon.isSolid)) {
    final outerShape = circle.containsPoint(polygon.globalVertices().first)
        ? circle
        : (polygon.containsPoint(circle.absoluteCenter) ? polygon : null);
    if (outerShape != null && outerShape.isSolid) {
      final innerShape = outerShape == circle ? polygon : circle;
      return [innerShape.absoluteCenter];
    }
  }
  return intersectionPoints;
}