Skip to content

Instantly share code, notes, and snippets.

@alexbilbie
Created March 26, 2013 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexbilbie/5247310 to your computer and use it in GitHub Desktop.
Save alexbilbie/5247310 to your computer and use it in GitHub Desktop.
$regions = [];
$potentialRegion = 0;
function adjacentPixelIsBlack($x, $y)
{
if ( ! isset($matrix[$x][$y+1]))
{
return false;
}
return ($matrix[$x][$y+1] === 1);
}
// Get all of the horizontal regions
foreach ($matrix as $row => $pixels) {
$x = $row;
$previousBlack = 0;
$regionStart = null;
foreach ($pixels as $y => $colour)
{
// If colour is white then just continue
if ($colour === 0)
{
if ($previousBlack === 1)
{
$regions[] = [$regionStart, [$x, $y]];
}
$previousBlack = 0;
continue;
}
if ($previousBlack === 1)
{
continue;
}
else
{
$regionStart = [$x, $y];
}
}
}
// Convert regions which are adjacent horizontally into just one region
// Can't work out how to do this bit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment