thin static method
Returns the indices to keep so that drawn label boxes — each centered at
centers[i] with width widths[i] — do not overlap, leaving at least
minGap between neighboring boxes. Greedy left-to-right; the first label
in position order is always kept.
Use for axis tick labels: a measured replacement for fixed stride-thinning that adapts to actual label widths instead of guessing a step.
Implementation
static List<int> thin(
List<double> centers,
List<double> widths,
double minGap,
) {
final n = centers.length;
if (n == 0) return const [];
final order = List<int>.generate(n, (i) => i)
..sort((a, b) => centers[a].compareTo(centers[b]));
final keep = <int>[];
var lastRight = double.negativeInfinity;
for (final idx in order) {
final left = centers[idx] - widths[idx] / 2;
final right = centers[idx] + widths[idx] / 2;
if (left >= lastRight + minGap) {
keep.add(idx);
lastRight = right;
}
}
keep.sort();
return keep;
}