Thanks for your reply. This is kinda what I came up with so far. It generates a pixel grid for X & Y coordinates.
IDK maybe someone someday will find this useful? I figured I would share. it works for what I needed.
GRID FUNCTION
<?php
public static function battleShipGrid($w, $h, $size)
{
$totX = intval(floor($w / $size));
$totY = intval(floor($h / $size));
$xRanges = array();
$yRanges = array();
$xLimit = 0;
while ($xLimit <= $totX) {
$start = intval($xLimit * $size);
$end = intval(($start + $size) - 1);
if ($end > $w)
$end = $w;
$xRanges[] = range($start, $end);
$xLimit++;
}
$yLimit = 0;
while ($yLimit <= $totY) {
$start = intval($yLimit * $size);
$end = intval(($start + $size) - 1);
if ($end > $h)
$end = $h;
$yRanges[] = range($start, $end);
$yLimit++;
}
return array("x" => $xRanges, "y" => $yRanges, "xTotal" => $totX, "yTotal" => $totY);
}
SEARCH
public static function searchPoint($xTest, $yTest, $xRanges, $yRanges)
{
$xResult = self::searchSquare($xTest, $xRanges);
$yResult = self::searchSquare($yTest, $yRanges);
return array("x" => $xResult, "y" => $yResult);
}
public static function searchSquare($needle, $haystack)
{
$found = false;
$current = 0;
while ($found === false) {
$x = $haystack[$current];
if (in_array($needle, $x))
$found = $current;
$current++;
if ($current > count($haystack))
$found = "not_found";
}
if ($found !== "not_found")
$result = $haystack[$found];
else
return false;
$min = $result[0];
$max = end($result);
return array("key" => $found, "min" => $min, "max" => $max, "items" => $result);
}
USAGE EXAMPLE
<?php
// Getting example coordinates like this
$examplePoint = "1910,1070";
$points = explode(",", $examplePoint);
$xTest = intval($points[0]);
$yTest = intval($points[1]);
// We can find out which X & Y Square they can be found in.
$xResult = self::searchSquare($xTest, $xRanges);
$yResult = self::searchSquare($yTest, $yRanges);
?>