Verify col & row selection

I’m trying to write a function to verify a users selection that is passed to a function. Basically I have a grid of pixels and each block is 10x10 pixels that users can choose. when the user has finished making their choices they click the next button. this is where i need to verify the selection because there are rules to selection…
The rule is that the selection must be rectangular in shape. (no L shaped selections, T shapes, ect) Hope that makes sense to you.

This is what i currently have… It seems to somewhat work. except that i cant seem to get it to check if each row of columns has the same number of selection.


<?php $selection = array(R4C21,R4C22,R4C23,R4C24,R4C25,R4C26,R5C21,R5C22,R5C23,R5C24,R5C25,R5C26,R6C21,R6C22,R6C23,R6C24,R6C25,R6C26); asort($selection); //sort the selection, its not going to be inorder echo IsSelectionValid($selection); function IsSelectionValid($selection) { foreach ($selection as $value) { $part = explode("C", $value); $col = $part[1]; $part2 = explode("R", $part[0]); $row = $part2[1]; if(!$lowcol) { $lowcol = $col; $nextcol = $col; } if(!$lowrow) { $lowrow = $row; $prevrow = $row - 1; } if($prevrow != $row) { $prevrow++; $colcount = 1; if($prevrow != $row) { return 'no'; } if($col != $lowcol) { return 'no'; } else { $nextcol = $col + 1; } echo $colcount; if($highcol && !$maxcol) { $maxcol = $highcol; echo 'Max Col: ' . $maxcol; } } else { if($nextcol != $col) { return 'no'; } $nextcol++; echo ' ' . $colcount++; $highcol = $colcount; //$prevcol++; //$currentrow = $row; //$nextrow = $row + 1; } echo '
' . $value . ' ROW: ' . $row . ' COL: ' . $col; } return 'yes'; } ?>


I think i have this thing figured out. I added counters to count each row and then counted all blocks selected…
Then i divided total number of blocks by the number of rows. Then doing doing a check to see if the result is a whole number i can determine if the selection was square.

[php]$boxes = $boxcount / $rowcount;
if(is_int($boxes)) {
return ‘yes’;
else {
return ‘no’;

I mean determine if the selection is rectangular is shape.

Sponsor our Newsletter | Privacy Policy | Terms of Service