forEachNeighbor8 method

void forEachNeighbor8(
  1. int idx,
  2. void visit(
    1. int neighborIdx
    )
)

Calls visit for each 8-connected neighbor index without allocating.

Implementation

@pragma('vm:prefer-inline')
void forEachNeighbor8(int idx, void Function(int neighborIdx) visit) {
  final up = hasNeighborUp(idx);
  final down = hasNeighborDown(idx);
  final left = hasNeighborLeft(idx);
  final right = hasNeighborRight(idx);
  if (up) visit(idx - width);
  if (down) visit(idx + width);
  if (left) visit(idx - 1);
  if (right) visit(idx + 1);
  if (up && left) visit(idx - width - 1);
  if (up && right) visit(idx - width + 1);
  if (down && left) visit(idx + width - 1);
  if (down && right) visit(idx + width + 1);
}