detectCorners function

List<int> detectCorners(
  1. CubicPath path, [
  2. double threshold = math.pi / 8
])

Implementation

List<int> detectCorners(CubicPath path, [double threshold = math.pi / 8]) {
  if (!threshold.isFinite || threshold < 0) {
    throw MeldException('invalid-corner-threshold',
        'Corner threshold must be finite and non-negative.');
  }
  final points = path.points;
  final segments = path.segmentCount;
  final active = <int>[
    for (var i = 0; i < segments; i++)
      if (_segmentLength(points, i) > 1e-9) i,
  ];
  if (active.isEmpty) return const <int>[];
  final corners = <int>{};
  void test(int a, int b) {
    final u = _tangent(points, a, true);
    final v = _tangent(points, b, false);
    if (u == null || v == null) return;
    final angle =
        math.atan2(u.$1 * v.$2 - u.$2 * v.$1, u.$1 * v.$1 + u.$2 * v.$2).abs();
    if (angle > threshold) corners.add(b);
  }

  for (var i = 0; i + 1 < active.length; i++) {
    test(active[i], active[i + 1]);
  }
  if (path.closed && active.length > 1) test(active.last, active.first);
  return corners.toList()..sort();
}