Skip to content

Instantly share code, notes, and snippets.

@LukeberryPi
Last active December 19, 2023 01:04
Show Gist options
  • Save LukeberryPi/c7a39c729cf0028df04eb478a645d046 to your computer and use it in GitHub Desktop.
Save LukeberryPi/c7a39c729cf0028df04eb478a645d046 to your computer and use it in GitHub Desktop.
function that returns if two coordinates are adjacent in javascript
function areAdjacent(coord1, coord2) {
const [x1, y1] = coord1;
const [x2, y2] = coord2;
const isHorizontallyAdjacent = Math.abs(x1 - x2) === 1 && y1 === y2;
const isVerticallyAdjacent = x1 === x2 && Math.abs(y1 - y2) === 1;
const isDiagonallyAdjacent =
Math.abs(x1 - x2) === 1 && Math.abs(y1 - y2) === 1;
return isHorizontallyAdjacent || isVerticallyAdjacent || isDiagonallyAdjacent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment