Last active
December 19, 2023 01:04
-
-
Save LukeberryPi/c7a39c729cf0028df04eb478a645d046 to your computer and use it in GitHub Desktop.
function that returns if two coordinates are adjacent in javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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