Skip to content

Instantly share code, notes, and snippets.

@bgadrian
Created March 16, 2015 17:07
Show Gist options
  • Save bgadrian/2b207e100f43a8d5eb5c to your computer and use it in GitHub Desktop.
Save bgadrian/2b207e100f43a8d5eb5c to your computer and use it in GitHub Desktop.
Checks / verify if 2 points are adjacent in a 2 dimension space
/** 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