isConvex property

bool get isConvex

Checks if this polygon is convex.

A polygon is convex if all interior angles are less than 180 degrees. This is a simplified check using cross products.

Note: This is a basic implementation and may not handle all edge cases (collinear points, etc.).

Implementation

bool get isConvex {
  if (points.length < 3) return false;

  bool hasPositive = false;
  bool hasNegative = false;

  for (int i = 0; i < points.length; i++) {
    final p1 = points[i];
    final p2 = points[(i + 1) % points.length];
    final p3 = points[(i + 2) % points.length];

    final crossProduct =
        (p2.longitude - p1.longitude) * (p3.latitude - p2.latitude) -
            (p2.latitude - p1.latitude) * (p3.longitude - p2.longitude);

    if (crossProduct > 0) hasPositive = true;
    if (crossProduct < 0) hasNegative = true;

    if (hasPositive && hasNegative) return false;
  }

  return true;
}