Source of: ellipse2.php


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Ellipse Generator</title>
<link rel="StyleSheet" href="simple1.css" type="text/css" media="screen" />
<style type="text/css" media="all"> @import "style1.css"; </style>
</head>
<body>
<h1>Ellipse Generator</h1>
<div class="deftext">
Here's a thing that draws ellipses (or circles, if you do it right) and even outputs paths for cutting them. Use at your own risk. Especially if you have a loose grip on your marbles.
<br />
<br />
And in the interest of science and late nights on the couch, <a href="ellipse2.html">here's</a> the code.
<ins><hr /></ins>
</div>
<table border="0" cellpadding="0"><tr><td class="top">
<?php

$dupx 
= $_POST['dupx'];   // how many horizontal
$dupy  = $_POST['dupy'];   // how many vertical
$rectx = $_POST['rectx'];  // ellipse width
$recty = $_POST['recty'];  // ellipse height
$kerf  = $_POST['kerf'];   // allowance for cutter
$clear = $_POST['clear'];  // space between pieces
$res   = $_POST['res'];    // resolution

$tried = $dupx || $dupy;
if (
$tried) {

 
$dupx=intval($dupx);
 
$dupy=intval($dupy);
 
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<table class="inputtable">
<tr><th class="red-s">(decimal inches only)</th></tr>
<tr><td>Ellipse width            </td><td><input type="text" maxlength="5" size="5" name="rectx" value="<?php if ($rectx<.1){$rectx = 1;echo $rectx; }else {echo $rectx;} ?>" /></td></tr>
<tr><td>Ellipse height           </td><td><input type="text" maxlength="5" size="5" name="recty" value="<?php if ($recty<.1){$recty = 2;echo $recty; }else {echo $recty;} ?>" /></td></tr>
<tr><td>Horizontal repeat<br />max = 8 </td><td><input type="text" maxlength="2" size="3" name="dupx" value="<?php if ($dupx<1 or $dupx>8){$dupx = 2;echo $dupx; }else {echo $dupx;} ?>" /></td></tr>
<tr><td>Vertical repeat<br />max = 8 </td><td><input type="text" maxlength="2" size="3" name="dupy" value="<?php if ($dupy<1 or $dupy>8){$dupy = 1;echo $dupy; }else {echo $dupy;} ?>" /></td></tr>
<tr><td>Cutter kerf offset     </td><td><input type="text" maxlength="5" size="5" name="kerf" value="<?php if ($kerf<.0001){$kerf = .03;echo $kerf; }else {echo $kerf;} ?>" /></td></tr>
<tr><td>Spacing for multiples  </td><td><input type="text" maxlength="5" size="5" name="clear" value="<?php if ($clear<.01){$clear = .375;echo $clear; }else {echo $clear;} ?>" /></td></tr>
<tr><td>Resolution             </td><td><input type="text" maxlength="4" size="5" name="res" value="<?php if ($res<1){$res = 1000;echo $res; }else {echo $res;} ?>" /></td></tr>
</table>
<p><input type="hidden" name="tried" value="yes" />
<input type="submit" value="<?php echo $tried ? 'recalculate' : 'calculate'; ?>" />
</p></form>
<?php

$clear
= $clear*$res;
$lead = 100;
$kerf2 = $kerf*2;
$kerfz = $kerf*$res*$zoom2;
$rad   = ($rectx+$kerf2)/2;
$rad2  = ($recty+$kerf2)/2;
$rectx = ($rectx+$kerf2)*$res;
$recty = ($recty+$kerf2)*$res;
$seg   = pi()/round(128);    // segment resolution
$start = pi()/4;             // pi is top, 0 is bottom, pi/4 is 45 deg. lead-in
$id = uniqid("");
$id1 = "a".$id;

$mystring = "SP1;\n";
$filename = "cache/$id1.txt";
touch($filename);
chmod($filename,0666);
$handle = fopen($filename, 'a');
fwrite($handle, $mystring);

for (
$c1 = 1$c1 <= $dupx; $c1++)
{
$y = 0;

for (
$c = 1$c <= $dupy; $c++)
{
$mystring = "\nPU" . (-round(sin(pi()/4)*$rad*$res)+round($x+($rectx/2)) -100) . " " . (-round(cos(pi()/4)*$rad2*$res)+round($y+($recty/2)) -100) .";\n";
fwrite($handle, $mystring);

for (
$i = $start$i <= 2*pi()+$start+.01; $i=$i+$seg)
{
$mystring = "PD". (-round(sin($i)*$rad*$res)+round($x+($rectx/2)))." ". (-round(cos($i)*$rad2*$res)+round($y+($recty/2))).";\n";
fwrite($handle, $mystring);
}
$y = round($y+$recty+$clear);
}
$x = round($x+$rectx+$clear);
}
$mystring = "\nSP0;\n";
fwrite($handle, $mystring);
fclose($handle);

$my_text  = $_POST['my_text'];
if (empty(
$my_text)){$my_text = @file_get_contents("cache/$id1.txt") ;}
?>

<form action="edit.php" method="post">
<p>
<textarea name="my_text" rows="7" cols="25"><?php echo $my_text; ?></textarea>
<br />
<input type="submit" value="edit code" />
<br />
</p>
</form>
<?php
echo "<a href=\"cache/$id1.txt\">Print HPGL output @ $res steps per inch</a><br />";

// make pitchers outta hpgl...

$xmin = 0;
$ymin = 0;
if (
file_exists("cache/$id1.txt"))
{
$fd = fopen("cache/$id1.txt", "r");
while (!
feof ($fd)) {
$buffer = fgets($fd, 4096);
if (
preg_match ("/p[ud](-?\d+) (-?\d+)/i", $buffer, $match)) {
$xmax = max($xmax, $match[1]);
$ymax = max($ymax, $match[2]);
$xmin = min($xmin, $match[1]);
$ymin = min($ymin, $match[2]);
$n = $n+1;
}
}

$res2 = max($xmax/500,$ymax/500,3);
$pad = max (40, -$xmin,-$ymin)/$res2;
$xdim = ($xmax-$xmin)/$res2+$pad;
$ydim = ($ymax-$ymin)/$res2+$pad;
$x2 = $pad;
$y2 = $ydim;
$pic=ImageCreate($xdim+$pad,$ydim+$pad);
$white=ImageColorAllocate($pic,255,255,255);
$trans=imagecolortransparent($pic,$white);
$green=ImageColorAllocate($pic,255,0,0); // red for line 1
$red=ImageColorAllocate($pic,255,0,0);
$black=ImageColorAllocate($pic,0,0,0);
ImageFilledRectangle($pic,$pad-2.4,$ydim-2.4,$pad+2.4,$ydim+2.4,$red);

$fd = fopen("cache/$id1.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);
$green=ImageColorAllocate($pic,20,225,100);
$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;
}
}
ImagePNG($pic,"cache/$id1.png");
ImageDestroy($pic);
}
// end make pitchers

echo "</td><td class=\"top\"> &nbsp; &nbsp; &nbsp; processed $n lines &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cutting area - ",round(($xmax-$xmin)/$res,1),"\" by ",round(($ymax-$ymin)/$res,1),"\"<br /><br /><img src=\"cache/$id1.png\" alt=\"picture 1\" /><br /><br />";

?>

</td></tr></table>
<div class="valid"><a href="http://validator.w3.org/check/referer">validate</a></div>
</body>
</html>