isConvex property

bool get isConvex

Whether this polygon is convex

All cross products of consecutive edges have same sign.

Implementation

bool get isConvex {
  final n = vertices.length;
  if (n < 3) return false;

  bool? positive;
  for (int i = 0; i < n; i++) {
    final a = vertices[i];
    final b = vertices[(i + 1) % n];
    final c = vertices[(i + 2) % n];
    final cross = (b.x - a.x) * (c.y - b.y) - (b.y - a.y) * (c.x - b.x);
    if (cross != 0) {
      if (positive == null) {
        positive = cross > 0;
      } else if ((cross > 0) != positive) {
        return false;
      }
    }
  }
  return true;
}