hitAt method

GraphHit? hitAt(
  1. Offset p
)

What is under p; null for nothing.

Implementation

GraphHit? hitAt(Offset p) {
  if (count == 0) return null;
  final byMark = spec.selection.by == 'mark';
  for (var pane = 0; pane < spec.panes.length; pane++) {
    final s = spec.panes[pane];
    final plot = plotRect(pane);
    if (p.dy < plot.top || p.dy >= plot.bottom) continue;
    if (s.polar) {
      for (var k = 0; k < s.marks.length; k++) {
        final m = s.marks[k];
        if (m.type != 'arc') continue;
        final (center, outer, inner) = arcGeometry(pane, m);
        final d = p - center;
        if (d.distance > outer + spec.selection.grow || d.distance < inner) continue;
        // Clockwise from the top.
        final angle = math.atan2(d.dx, -d.dy);
        for (final slice in slices(m)) {
          final turned = (angle - slice.start) % (2 * math.pi);
          if (turned <= slice.sweep) return GraphHit(pane: pane, index: slice.index, mark: k);
        }
      }
      return null;
    }
    if (p.dx < plot.left || p.dx > plot.right) return null;
    final index = rowAt(p.dx);
    if (index < 0 || index >= count) return null;
    if (!byMark) return GraphHit(pane: pane, index: index);
    (int, double)? best;
    for (final (k, r) in barRects(pane, index)) {
      if (p.dx < r.left || p.dx > r.right) continue;
      final off = p.dy < r.top ? r.top - p.dy : (p.dy > r.bottom ? p.dy - r.bottom : 0.0);
      if (best == null || off < best.$2) best = (k, off);
    }
    return best == null ? null : GraphHit(pane: pane, index: index, mark: best.$1);
  }
  return null;
}