I’m looking for some sort of formula that, for each i from 0 to 7 will return an x and a y offset to an adjacent cell in a certain direction. The idea is that if I’m in a grid of cells, and I want to scan the surrounding cells, I don’t have to make a christmas tree of if statements (much, much slower than arithmatic). Note that this scan includes the diagonals. I’ve been looking online for something like this, but with no luck.
The directions can be output in any order as long as each input yields a different output, x and y can only equal 1, 0 or -1, and none of the outputs are (0, 0).
Assuming that
x,yis original coordinate andnx,nywill be the current neighbour:or just use constants: