<?php
$id = uniqid("");
$d1 = $_POST['d1']; $thick = $_POST['thick']; $d2 = $_POST['d2']; $ang = $_POST['angle']; $F = $_POST['F']; $tried = $d1;
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<table class="inputtable">
<tr><td>Diameter 1<br />(cut tube)</td><td><input type="text"
maxlength="5" size="5" name="d1" value="<?php if ($d1<.1 || $d1>10)
{$d1 = 1;echo $d1; }else {echo $d1;} ?>" /></td></tr>
<tr><td>Wall Thickness<br />(cut tube)</td><td><input type="text"
maxlength="5" size="5" name="thick" value="<?php if ($thick == 0 || $thick < 0 || (2 * $thick) > $d1)
{$thick = ".065"; echo $thick; } else {echo $thick;} ?>" /></td></tr>
<tr><td>Diameter 2</td><td><input type="text" maxlength="5"
size="5" name="d2" value="<?php if ($d2<.1 || $d2>10)
{$d2 = 1;echo $d2; }else {echo $d2;} ?>" /></td></tr>
<tr><td>Angle</td><td><input type="text" maxlength="5"
size="5" name="angle" value="<?php if ($ang < 1 || $ang > 90)
{$ang = 90; echo $ang;} else {echo $ang;} ?>" /></td></tr>
<tr><td>Offset</td><td><input type="text" maxlength="5"
size="5" name="F" value="<?php echo round ($F,3); ?>" />
</td></tr>
</table>
<p>
<input type="submit" value="<?php echo $tried ? 'recalculate' : 'calculate'; ?>" />
</p></form>
<?php
echo "<h3>Download pattern:</h3><div>
<a href=\"/cache/$id.pdf\">PDF</a>
<br /><a href=\"/cache/$id.png\">PNG</a>
<br /><a href=\"/cache/$id.ps\">Postscript</a>
<br /><a href=\"/cache/$id.txt\">HPGL</a></div>";
$A = deg2rad(90-$ang);
$R1 = $d1/2; $R1id = $R1 - $thick; $R2 = $d2/2; $R2sqr = $R2 * $R2;
$m = tan($A); $m2 = tan($A / 2); $segs = 144; $inc = pi() * 2 / $segs;
$mystring = "SP1;\n";
$filename = "$_SERVER[DOCUMENT_ROOT]/cache/$id.txt";
touch($filename);
chmod($filename,0666);
$handle = fopen($filename, 'a');
fwrite($handle, $mystring);
for ( $i = 0; $i <= $segs; $i++ ) {
$theta = -pi()/2 + $i * $inc;
$x = $R1 * cos($theta) - $F; $inx = $R1id * cos($theta) - $F;
$y = $R1 * sin($theta); $iny = $R1id * sin($theta);
if ( $R2 == 0 ) {
$z = 0; $b = 0;
}
else {
$d = $R2sqr - $x * $x; $ind = $R2sqr - ($inx * $inx);
$z = $d < 0.0 ? 0.0 : sqrt($d); $inz = $ind < 0.0 ? 0.0 : sqrt($ind);
$b = $m * $m2 * $z; $inb = $m * $m2 * $inz;
}
$z += $y * $m + $b; $inz += $iny * $m + $inb;
$x += $F; $z = $R2 - $z; $inz = $R2 - $inz;
$z = min($z,$inz);
if (!isset($set)){$set = $z;}
$z = $z-$set;
$zr = round ($z*1000);
$zr = -$zr;
$l = $i * $inc * $R1;
$lr = round ($l*1000);
$mystring= "PD$zr $lr;\n";
fwrite($handle, $mystring);
}
$mystring = "\nSP0;\n";
fwrite($handle, $mystring);
fclose($handle);
$my_text = $_POST['my_text'];
if (empty($my_text)){$my_text = @file_get_contents("$_SERVER[DOCUMENT_ROOT]/cache/$id.txt") ;}
$xmin = 0;
$ymin = 0;
if (file_exists("$_SERVER[DOCUMENT_ROOT]/cache/$id.txt"))
{
$fd = fopen("$_SERVER[DOCUMENT_ROOT]/cache/$id.txt", "r");
while (!feof ($fd)) {
$buffer = fgets($fd, 4096);
if (preg_match ("/p[ud](-?\d+) (-?\d+)/i", $buffer, $match)) {
$xmax = max($xmax, $match[1]);
$xmax2 = max($xmax, $match[1],2000,$d1*$d2*50);
$ymax = max($ymax, $match[2]);
$xmin = min($xmin, $match[1]);
$ymin = min($ymin, $match[2]);
$n = $n+1;
}
}
$res2 = 10;
$pad = max (10, -$xmin,-$ymin)/$res2;
$xdim = ($xmax2-$xmin)/$res2+$pad;
$ydim = ($ymax-$ymin)/$res2;
$x2 = $pad;
$y2 = $ydim;
$pic=ImageCreate($xdim+$pad,$ydim+$pad);
$white=ImageColorAllocate($pic,255,255,255);
$trans=imagecolortransparent($pic,$white);
$green=ImageColorAllocate($pic,20,225,100);
$red=ImageColorAllocate($pic,255,0,0);
$black=ImageColorAllocate($pic,0,0,0);
$fd = fopen("$_SERVER[DOCUMENT_ROOT]/cache/$id.txt", "r");
while (!feof ($fd)) {
$buffer = fgets($fd, 4096);
if (preg_match ("/pu(-?\d+) (-?\d+)/i", $buffer, $match)) {
$x1 = $match[1]/$res2+$pad;
$y1 = $ydim-($match[2]/$res2);
ImageLine($pic,$x1,$y1,$x2,$y2,$green);
$x2 = $x1;
$y2 = $y1;
}
elseif (preg_match ("/pd(-?\d+) (-?\d+)/i", $buffer, $match)) {
$x1 = $match[1]/$res2+$pad;
$y1 = $ydim-($match[2]/$res2);
ImageLine($pic,$x1,$y1,$x2,$y2,$black);
$x2 = $x1;
$y2 = $y1;
}
}
ImageLine($pic,$pad,0,$xmax2*2.1/$res2,0,$green);
ImageLine($pic,$pad,$ydim,$xmax2*2.1/$res2,$ydim,$green);
imagestring ( $pic, 2, $xdim-98, 10, "Cut tube: $d1\"", $red);
if ($thick > 0) {imagestring ( $pic, 2, $xdim-134, 22, "Wall thickness: $thick\"", $red);
$space =12;}
imagestring ( $pic, 2, $xdim-116, 22 + $space, "Parent tube: $d2\"", $red);
imagestring ( $pic, 2, $xdim-80, 34 + $space, "Angle: $ang deg", $red);
if ($F > 0) {imagestring ( $pic, 2, $xdim-86, 46 + $space, "Offset: $F\"", $red);}
ImagePNG($pic,"$_SERVER[DOCUMENT_ROOT]/cache/$id.png");
ImageDestroy($pic);
$xmax3 = $xmax2/1000 + .75;
$xmax4 = $xmax2/1000 - 1.25;
$ps =
"%!PS-Adobe-2.0
initgraphics
72 dup scale
0 11 translate
-90 rotate
1.0 1.0 translate
0.004 setlinewidth
/Courier findfont 10 72 div scalefont setfont
gsave
90 rotate
0 -2 translate
newpath $xmax4 1.8 moveto ( Cut tube: $d1\") show
newpath $xmax4 1.6 moveto (Wall thickness: $thick\") show
newpath $xmax4 1.4 moveto ( Parent tube: $d2\") show
newpath $xmax4 1.2 moveto ( Angle: $ang deg) show
newpath $xmax4 1.0 moveto ( Offset: $F\") show
grestore
newpath 0 $xmax3 moveto\n";
$fd = fopen("$_SERVER[DOCUMENT_ROOT]/cache/$id.txt", "r");
while (!feof ($fd)) {
$buffer = fgets($fd, 4096);
if (preg_match ("/pu(-?\d+) (-?\d+)/i", $buffer, $match)) {
$xps =($match[1]-$xmin)/1000;
$yps =($match[2])/1000;
$ps = $ps."$yps $xps lineto\n";
}
elseif (preg_match ("/pd(-?\d+) (-?\d+)/i", $buffer, $match)) {
$xps =($match[1]-$xmin)/1000;
$yps =($match[2])/1000;
$ps = $ps."$yps $xps lineto\n";
}
}
$ps = $ps.
"$yps $xmax3 lineto
0 $xmax3 lineto
stroke
";
$filename = "$_SERVER[DOCUMENT_ROOT]/cache/$id.ps";
touch($filename);
chmod($filename,0666);
$handle = fopen($filename, 'a');
fwrite($handle, $ps);
fclose($handle);
$convert_pdf =
` convert -scale 100% $_SERVER[DOCUMENT_ROOT]/cache/$id.ps $_SERVER[DOCUMENT_ROOT]/cache/$id.pdf `;
echo $convert_pdf;
}
echo "</div><div class=\"float-lt\">
pattern height: ",round($d1 * pi(),2),"\"<br /><br />
<a href=\"/cache/$id.png\"><img src=\"/cache/$id.png\" alt=\"coping pattern\" /></a>";
?>