splitPoly function
Cut a polygon along the line through (x, y) at angle a: the pieces with three corners or more.
Implementation
List<List<(double, double)>> splitPoly(
List<(double, double)> pts,
double x,
double y,
double a,
) {
final nx = -math.sin(a);
final ny = math.cos(a);
double side((double, double) q) => (q.$1 - x) * nx + (q.$2 - y) * ny;
final left = <(double, double)>[];
final right = <(double, double)>[];
for (var i = 0; i < pts.length; i++) {
final p = pts[i];
final q = pts[(i + 1) % pts.length];
final sp = side(p);
final sq = side(q);
(sp >= 0.0 ? left : right).add(p);
if ((sp >= 0.0) != (sq >= 0.0)) {
final u = sp / (sp - sq);
final m = (p.$1 + (q.$1 - p.$1) * u, p.$2 + (q.$2 - p.$2) * u);
left.add(m);
right.add(m);
}
}
return [
if (left.length >= 3) left,
if (right.length >= 3) right,
];
}