rectToPolygon static method

bool rectToPolygon(
  1. RectangleShape a,
  2. PolygonShape b
)

Implementation

static bool rectToPolygon(RectangleShape a, PolygonShape b) {
  if (!rectToRect(a, b.rect)) return false;

  if (!isLinesShadowOver(
    a.leftTop,
    a.rightBottom,
    b.rect.leftTop,
    b.rect.rightBottom,
  )) return false;

  if (polygonPoint(b, a.position)) {
    return true;
  }

  final pointsA = [
    a.leftTop,
    a.rightTop,
    a.rightBottom,
    a.leftBottom,
    a.leftTop,
  ];
  final pointsB = b.points.toList()..add(b.points.first);

  for (var i = 0; i < pointsA.length - 1; i++) {
    final pointA = pointsA[i];
    final pointB = pointsA[i + 1];
    for (var j = 0; j < pointsB.length - 1; j++) {
      final pointC = pointsB[j];
      final pointD = pointsB[j + 1];

      if (!isLinesShadowOver(pointA, pointB, pointC, pointD)) {
        continue;
      }

      if (isLinesOver(pointA, pointB, pointC, pointD)) {
        return true;
      }
    }
  }

  return false;
}