Originally Posted by atl77
Sure thing!
Code:
<?php
function newgrid() {
$grid = array();
for ($x = 0; $x < 100; $x++) {
$grid[$x] = array();
for ($y = 0; $y < 100; $y++) {
$grid[$x][$y] = 0;
}
}
return $grid;
}
function coords2grid($coords, $radius = 5, $shrink) {
$grid = newgrid();
for ($c = 0; $c < count($coords); $c++) {
$cx = round($coords[$c]['x']);
$cy = round($coords[$c]['y']);
$rx = $radius;
$ry = 0;
$rerr = $radius;
for ($ln = $rx; $ln > 0; $ln--) {
if ($cx+$rx-$ln >= 0 and $cx+$rx-$ln < 100 and $cy+$ry >= 0 and $cy+$ry < 100) { $grid[$cx+$rx-$ln][$cy+$ry] = 1; }
if ($cx-$rx+$ln >= 0 and $cx-$rx+$ln < 100 and $cy+$ry >= 0 and $cy+$ry < 100) { $grid[$cx-$rx+$ln][$cy+$ry] = 1; }
if ($cx-$ry >= 0 and $cx-$ry < 100 and $cy-$rx+$ln >= 0 and $cy-$rx+$ln < 100) { $grid[$cx-$ry][$cy-$rx+$ln] = 1; }
if ($cx+$ry >= 0 and $cx+$ry < 100 and $cy-$rx+$ln >= 0 and $cy-$rx+$ln < 100) { $grid[$cx+$ry][$cy-$rx+$ln] = 1; }
}
while ($ry < $rx) {
$dy = $ry * 2 + 1;
$ry++;
$rerr = $rerr - $dy;
if ($rerr < 0) {
$dx = 1 - $rx * 2;
$rx--;
$rerr = $rerr - $dx;
}
for ($ln = $rx; $ln > 0; $ln--) {
if ($cx+$rx-$ln >= 0 and $cx+$rx-$ln < 100 and $cy+$ry >= 0 and $cy+$ry < 100) { $grid[$cx+$rx-$ln][$cy+$ry] = 1; }
if ($cx-$rx+$ln >= 0 and $cx-$rx+$ln < 100 and $cy+$ry >= 0 and $cy+$ry < 100) { $grid[$cx-$rx+$ln][$cy+$ry] = 1; }
if ($cx-$ry >= 0 and $cx-$ry < 100 and $cy-$rx+$ln >= 0 and $cy-$rx+$ln < 100) { $grid[$cx-$ry][$cy-$rx+$ln] = 1; }
if ($cx+$ry >= 0 and $cx+$ry < 100 and $cy-$rx+$ln >= 0 and $cy-$rx+$ln < 100) { $grid[$cx+$ry][$cy-$rx+$ln] = 1; }
}
}
}
if (!isset($shrink)) { $shrink = $radius - 1; }
for ($s = $shrink; $s >= 0; $s--) {
$grid2 = newgrid();
for ($x = 1; $x < 99; $x++) {
for ($y = 1; $y < 99; $y++) {
$grid2[$x][$y] = ($grid[$x][$y] and ($grid[$x+1][$y-1] + $grid[$x+1][$y] + $grid[$x+1][$y+1] + $grid[$x][$y-1] + $grid[$x][$y+1] + $grid[$x-1][$y-1] + $grid[$x-1][$y] + $grid[$x-1][$y+1]) > 4) ? 1 : 0;
}
}
$grid = $grid2;
}
return $grid;
}
function grid2boxes($grid) {
$boxes = array();
$currentbox = false;
for ($y = 0; $y < 100; $y++) {
for ($x = 0; $x < 100; $x++) {
if ($currentbox != false) {
if ($grid[$x][$y] == 1) {
$currentbox['width']++;
} else {
array_push($boxes, $currentbox);
$currentbox = false;
}
} else {
if ($grid[$x][$y] == 1) {
$currentbox = array("x" => $x, "y" => $y, "width" => 1, "height" => 1);
}
}
}
}
return $boxes;
}
function showgrid($grid) {
$output = "";
for ($y = 0; $y < 100; $y++) {
for ($x = 0; $x < 100; $x++) {
$output .= $grid[$x][$y] == 0 ? ' ' : 'X';
}
$output .= "\n";
}
return $output;
}
// var_dump(grid2boxes(coords2grid(array(0 => array('x' => 33.0, 'y' => 33.0), 1 => array('x' => 35.0, 'y' => 36.0)))));
print showgrid(coords2grid(array(0 => array('x' => 33.0, 'y' => 33.0), 1 => array('x' => 35.0, 'y' => 36.0))));
?>
|
GREAT!!! Ill implement this to my code today.