Created
March 16, 2015 17:07
-
-
Save bgadrian/2b207e100f43a8d5eb5c to your computer and use it in GitHub Desktop.
Checks / verify if 2 points are adjacent in a 2 dimension space
This file contains hidden or 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
/** Calculates, from two set of coord, if two nodes are adjacents. | |
* @return bool|null True if they are adiacents, null if the coords are not valid or are the same. | |
*/ | |
public static function areNodeAdjacents($coord_1_norm,$coord_2_norm) | |
{ | |
if ($coord_1_norm['x'] === $coord_2_norm['x'] AND $coord_1_norm['y'] === $coord_2_norm['y'])//or is the same planet .. | |
return null; | |
//the formula | |
if (max(abs($coord_1_norm['x']-$coord_2_norm['x']),abs($coord_1_norm['y']-$coord_2_norm['y'])) <= 1) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment