area method Null safety

double area(
  1. List<Point<num>> polygon
)

Get Polygon area

This algorithm could be wrong when work with polygon that contains crossed lines

Implementation

static double area(List<Point> polygon) {
  assert(polygon.length >= 3);

  final n = polygon.length;
  num x = 0, y = 0;

  for (var i = 0; i < n; i++) {
    final nextI = (i + 1) % n;
    x += polygon[i].x * polygon[nextI].y;
    y += polygon[i].y * polygon[nextI].x;
  }

  return (x - y) / 2;
}