Created
May 17, 2012 19:02
-
-
Save oranj/2720929 to your computer and use it in GitHub Desktop.
Generate Sphere
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$is_shell_script = ! isset($_SERVER['REQUEST_URI']); | |
$width = $height = $depth = 5; | |
$hollow = false; | |
if ($is_shell_script) { | |
for ($i = 1; $i < $argc; $i++) { | |
if (preg_match('/\-([a-z])/i', $argv[$i], $matches)) { | |
$flag = $matches[1]; | |
switch ($flag) { | |
case 'o': | |
$hollow = true; | |
break; | |
case 'w': | |
$i++; | |
$width = $argv[$i]; | |
break; | |
case 'h': | |
$i++; | |
$height = $argv[$i]; | |
break; | |
case 'd': | |
$i++; | |
$depth = $argv[$i]; | |
break; | |
} | |
} | |
} | |
} | |
$width --; | |
$height --; | |
$depth --; | |
function generate_sphere($x_radius, $y_radius, $z_radius, $hollow = false, $tolerance = 0.1, $thickness = 0.5) { | |
$sphere = Array(); | |
$radius = min($x_radius, $y_radius, $z_radius); | |
$x_ratio = $x_radius / $radius; | |
$y_ratio = $y_radius / $radius; | |
$z_ratio = $z_radius / $radius; | |
for ($z = -$z_radius; $z <= $z_radius; $z++) { | |
for ($y = -$y_radius; $y <= $y_radius; $y++) { | |
for ($x = -$x_radius; $x <= $x_radius; $x++) { | |
$xP = $x / $x_ratio; | |
$yP = $y / $y_ratio; | |
$zP = $z / $z_ratio; | |
$distance = sqrt($xP*$xP + $yP*$yP + $zP*$zP); | |
if ($hollow) { | |
$solid = abs($distance - $radius) <= $thickness; | |
} else { | |
$solid = ($distance - $tolerance) < $radius; | |
} | |
$sphere[(string)$z][(string)$y][(string)$x] = $solid; | |
} | |
} | |
} | |
return $sphere; | |
} | |
$model = generate_sphere($width / 2, $depth / 2, $height / 2, $hollow); | |
if ($is_shell_script) { | |
foreach ($model as $z => $plane) { | |
foreach ($plane as $y => $row) { | |
foreach ($row as $x => $col) { | |
echo $col?'#':' '; | |
} | |
echo "\n"; | |
} | |
echo "\n"; | |
} | |
} else { | |
foreach ($model as $z => $plane) { | |
echo '<table>'; | |
foreach ($plane as $x => $row) { | |
echo '<tr>'; | |
foreach ($row as $y => $col) { | |
echo '<td '.($col?'class="active"':'').'></td>'; | |
} | |
echo '</tr>'; | |
} | |
echo '</table>'; | |
echo '<br/>'; | |
} | |
?> | |
<style> | |
table { | |
border-collapse:collapse; | |
} | |
td { | |
width:10px; height:10px; | |
border:1px solid #000; | |
} | |
td.active { | |
border-color:#fff; | |
background-color:#000; | |
} | |
</style><?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment