sortSlotsByDir function

List<Rect> sortSlotsByDir(
  1. List<Rect> slots,
  2. Offset anchorPx,
  3. Offset dir
)

Implementation

@pragma('vm:prefer-inline')
List<Rect> sortSlotsByDir(List<Rect> slots, Offset anchorPx, Offset dir) {
  final nd = dir.distance > 1e-6 ? dir / dir.distance : const Offset(1, 0);

  double score(Rect r) {
    final c = r.center - anchorPx;
    final nc = c.distance > 1e-6 ? c / c.distance : Offset.zero;
    return nc.dx * nd.dx + nc.dy * nd.dy;
  }

  slots.sort((a, b) {
    final sa = score(a);
    final sb = score(b);
    if (sa != sb) return sb.compareTo(sa);
    final areaA = a.width * a.height;
    final areaB = b.width * b.height;
    if (areaA != areaB) return areaB.compareTo(areaA);
    return a.hashCode.compareTo(b.hashCode);
  });
  return slots;
}