0 and $_GET['h'] < 1.5) ? $_GET['h'] : 0.75; $studs = !($_GET['b']==1); $jumpers = ($_GET['j']==1); $smallcore = ($_GET['s']==1); $complete = !($_GET['p']==1); $radius = ($_GET['d']>=1.6 and $_GET['d']<$MAX) ? $_GET['d']/2 : 0.8; $color = ($_GET['c']==1) ? array(1,14,4) : false; $plate = 0.4; if($smallcore) $core_min = ceil(1/3*(sqrt(2*(6*sq($radius)-4*sq($half*$plate)-16*($half+1)*sq($plate)-3))-2*$half*$plate-4*$plate)); $core_max = floor(1/3*(sqrt(2*(6*sq($radius)-4*sq($half*$plate)-4*$half*$plate-1))-2*$half*$plate+2)); if($smallcore and $core_min > 0) $core = $core_min; else $core = $core_max; $diam = round(2*$radius,1); $name = "BramSphere_D{$diam}_with_{$core}x{$core}x{$core}_core"; if($complete) $name.=".mpd"; else $name.=".ldr"; header('Content-type: application/x-ldraw'); header('Content-Disposition: inline; filename="'.$name.'"'); echo ldrawmpd($core,$radius,$jumpers,$studs,$half,$plate,$color,$complete); // Supporting functions follow below: // // * * * LDraw MPD Creation functions * * * // function ldrawmpd($core,$radius,$jumpers,$studs,$half,$plate,$clr,$complete) { $diam = round(2*$radius,1); $name = "sphe_d{$diam}_c{$core}"; $year = date('Y'); $mpd = ""; if($complete) { $mpd.= << 0 For personal private use only. 0 Do not redistribute without written permission. 0 TEXT; $mpd.= ldrawsphere($core,$name."_sub.ldr"); $mpd.= << 0 For personal private use only. 0 Do not redistribute without written permission. 0 TEXT; $mpd.= ldrawpanel(geometry($core,$radius,$jumpers,$half,$plate),$core,$studs,$clr); $mpd.= "0\n"; return $mpd; } function ldrawsphere($core,$panelname) { $x = $core*10; $code = ""; $code.= " 1 16 0 -$x 0 1 0 0 0 1 0 0 0 1 $panelname\n"; $code.= " 1 16 0 $x 0 -1 0 0 0 -1 0 0 0 1 $panelname\n"; $code.= " 1 16 $x 0 0 0 -1 0 0 0 1 -1 0 0 $panelname\n"; $code.= " 1 16 -$x 0 0 0 1 0 0 0 1 1 0 0 $panelname\n"; $code.= " 1 16 0 0 $x 0 0 -1 1 0 0 0 -1 0 $panelname\n"; $code.= " 1 16 0 0 -$x 0 0 1 1 0 0 0 1 0 $panelname\n"; return $code; } function ldrawpanel($level,$core,$studs,$colors=false) { $code = ""; $ht = count($level); // number of plates high $wd = count($level[0]); //number of plates wide for($i=0;$i<$ht;$i++){ if($colors[0]) $c = $colors[$i % count($colors)]; else $c = 16; $y = -4 - 8*$i; for($j=0;$j<$wd;$j++){ $z = $level[$i][$j]*20; $x = (-($core%2)*0.5 + 0.5 + $j)*20; if($z > 0) { if($x > 0) $code.=" 1 $c -$x $y 0 10 0 0 0 4 0 0 0 $z box.dat\n"; $code.=" 1 $c $x $y 0 10 0 0 0 4 0 0 0 $z box.dat\n"; if($studs){ $cur = $level[$i][$j]; $nxt = $level[$i+1][$j]; $ys = $y-4; if($i < $ht-1 and (($cur-$nxt)>=1 or $nxt==0)){ for($k=$cur;$k>$nxt+0.5 or ($nxt==0 and $k>0);$k--){ $zs = ($k-0.5)*20; if($k>0.5){ if($x>0){ $code.=" 1 $c -$x $ys $zs 1 0 0 0 1 0 0 0 1 stud.dat\n"; $code.=" 1 $c -$x $ys -$zs 1 0 0 0 1 0 0 0 1 stud.dat\n"; } $code.=" 1 $c $x $ys $zs 1 0 0 0 1 0 0 0 1 stud.dat\n"; $code.=" 1 $c $x $ys -$zs 1 0 0 0 1 0 0 0 1 stud.dat\n"; } elseif($k==0.5) { if($x>0) $code.=" 1 $c -$x $ys 0 1 0 0 0 1 0 0 0 1 stud.dat\n"; $code.=" 1 $c $x $ys 0 1 0 0 0 1 0 0 0 1 stud.dat\n"; } } } elseif ($i==$ht-1) { for($k=$cur;$k>0;$k--){ $zs = ($k-0.5)*20; if($k>0.5){ if($x>0){ $code.=" 1 $c -$x $ys $zs 1 0 0 0 1 0 0 0 1 stud.dat\n"; $code.=" 1 $c -$x $ys -$zs 1 0 0 0 1 0 0 0 1 stud.dat\n"; } $code.=" 1 $c $x $ys $zs 1 0 0 0 1 0 0 0 1 stud.dat\n"; $code.=" 1 $c $x $ys -$zs 1 0 0 0 1 0 0 0 1 stud.dat\n"; } elseif($k==0.5) { if($x>0) $code.=" 1 $c -$x $ys 0 1 0 0 0 1 0 0 0 1 stud.dat\n"; $code.=" 1 $c $x $ys 0 1 0 0 0 1 0 0 0 1 stud.dat\n"; } } } } } } } return $code; } // // * * * Calculation of sphere shape * * * // function geometry($core,$radius,$jumpers,$half,$plate) { if($core % 2) // odd number of studs, count from middle $xmin = 0; else // even number of studs, $xmin = 0.5; $level = array(); for($h=$core/2+$plate*$half;$h<=$radius;$h+=$plate) { $r_sqr = sq($radius)-sq($h); $line = array(); for($x=$xmin;$x<=$core/2;$x+=1){ if(sq($x)>$r_sqr) $ln = 0; else { $ln = sqrt($r_sqr-sq($x)); if($jumpers and $ln>$core/2 and (round(2*$ln)>=4 or round(2*$ln)<=2)) $ln = 0.5*round(2*$ln); else{ if($core % 2) $ln = round($ln+0.5)-0.5; else $ln = round($ln); } } $line[] = $ln; } $level[] = $line; } return $level; } // // * * * Other functions * * * // // sq: // squares a number function sq($x) { return $x*$x; } ?>