If I collapsed a 2 dimensional grid into 1 dimension (e.g a 3x3 grid into a string):
1 2 3
4 5 6 => 1 2 3 4 5 6 7 8 9
7 8 9
Is there a formula where I could verify if two points used to be neighbors?
Working it out on a markerboard, the best I could come up with is:
(a % r) - (b % r) = {0,1}
Where a & b are the points and r is the row length.
This works well enough for handling the wrapping problem (e.g. 3 and 4 are not neighbors). But it thinks everything in the same column are neighbors. This formula would say that 1 and 7 are neighbors because (1%3)-(7%3) => 1-1 => 0.
Is there a way to correct that? I have no idea what I'm doing, so I may be approaching this the wrong way. I would not be surprised if there was an existing algorithm, and that ultimately is what I am after. I don't need my formula fixed, I'm just providing it to hopefully explain whats going on.