/ Published in: JavaScript
This function returns true if the two sets of coordinates are neighbors and false if they ar not. The function easily determines whether they are neighbors by looking at the distance between the positions along both axes, also callled the Manhattan distance. The sum of the two distances must be exactly 1 if the positions are adjacent.
Note: the coordinates should be integers
Expand |
Embed | Plain Text
// returns true if (x1,y1) is adjacent to (x2,y2) function isAjdacent(x1, y1, x2, y2) { var dx = Math.abs(x1 - x2), dy = Math.abs(y1 - y2); return (dx + dy === 1); }
You need to login to post a comment.
