March 27, 2004

Ellipse generator

Yet another in my series of thingies rather crudely copy/pasted from an old project of mine.

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.

(decimal inches only)
Ellipse width
Ellipse height
Horizontal repeat
max = 8
Vertical repeat
max = 8
Cutter kerf offset
Spacing for multiples
Resolution

Print HPGL output @ 1000 steps per inch
      processed 516 lines             cutting area - 2.5" by 2.1"

picture 1

Source:


<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 />

<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 = "$_SERVER[DOCUMENT_ROOT]/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("$_SERVER[DOCUMENT_ROOT]/cache/$id1.txt") ;}
?>

<form>
<p>
<textarea name="my_text" rows="7" cols="25"><?php echo $my_text; ?></textarea>
<br />

<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("$_SERVER[DOCUMENT_ROOT]/cache/$id1.txt"))
{
$fd = fopen("$_SERVER[DOCUMENT_ROOT]/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("$_SERVER[DOCUMENT_ROOT]/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,"$_SERVER[DOCUMENT_ROOT]/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>
Posted by Hal Eckhart at March 27, 2004 09:42 AM | TrackBack
Comments
Post a comment
spam and annoying comments will be deleted







Remember personal info?




enter this number security code in the box below (prevents comment spam)