ledgerInterp function

double? Function(double x) ledgerInterp(
  1. List<Offset> points
)

art.interp: the polyline through points as a function of x, null outside its span.

Implementation

double? Function(double x) ledgerInterp(List<Offset> points) {
  final xs = [for (final p in points) p.dx];
  return (x) {
    if (x < xs.first - 1e-9 || x > xs.last + 1e-9) return null;
    // The first segment holding x, as the source walks them: one before
    // the first point at or past x.
    var lo = 0, hi = xs.length;
    while (lo < hi) {
      final mid = (lo + hi) >> 1;
      if (xs[mid] < x) {
        lo = mid + 1;
      } else {
        hi = mid;
      }
    }
    final i = math.max(0, lo - 1);
    if (i + 1 >= points.length) return points.last.dy;
    final a = points[i], b = points[i + 1];
    if (x < a.dx || x > b.dx) return points.last.dy;
    if (b.dx == a.dx) return a.dy;
    return a.dy + (b.dy - a.dy) * (x - a.dx) / (b.dx - a.dx);
  };
}