Points on a circle primitive
2012-01-08, 21:51 (This post was last modified: 2012-01-09, 11:53 by Tim Gould.)
2012-01-08, 21:51 (This post was last modified: 2012-01-09, 11:53 by Tim Gould.)
Hi all,
Below is a perl script to take a bunch of angles and convert them to points on the edge of a circle primitve (ie. a 16 or 48 point polygon). The comments up top describe how to use it. It's designed to make generating rims on the bottom of curved parts relatively easy when there's only a few segments required and hand-coding makes more sense than using Philo's wonderful toys.
Tim
Below is a perl script to take a bunch of angles and convert them to points on the edge of a circle primitve (ie. a 16 or 48 point polygon). The comments up top describe how to use it. It's designed to make generating rims on the bottom of curved parts relatively easy when there's only a few segments required and hand-coding makes more sense than using Philo's wonderful toys.
Tim
Code:
#! /usr/bin/perl
# ang2point.pl [-r RAD] [-t THICK] [-s SEGS] ang1 ang2 ... angn
#
# RAD defaults to 20
# THICK defaults to none
# SEGS defaults to 16
#
# Projects the angles onto a circular primitive of radius RAD
# with SEGS segments. If THICK is specified it also shows the
# points on an inner radius of RAD-THICK
#
# It scales the radius at each angle to ensure it lies on the
# SEG-sided polygon that approximates the circle.
#
use POSIX;
$rad=20;
$segs=16;
@angs=();
$thick=0;
for ($ia=0; $ia<=$#ARGV; $ia++) {
$opt=lc($ARGV[$ia]);
if ($opt eq '-r') {
$ia++;
$rad=$ARGV[$ia];
} elsif ($opt eq '-t') {
$ia++;
$thick=$ARGV[$ia];
} elsif ($opt eq '-s') {
$ia++;
$segs=$ARGV[$ia];
} else {
push @angs, $opt;
}
}
$nang=$#angs+1;
print "0 // ang2point.pl ".join(' ', @ARGV)."\n";
print "0 // Calculating $nang points at outer radius $rad on $segs segments\n";
if ($thick>0) {
$radp=$rad-$thick;
print "0 // Also calculating inner radius $radp\n";
}
@reffs=();
@rieffs=();
$kk=3.141592635/180;
foreach $ang (@angs) {
$f=$ang/360;
$a0=floor($f*$segs)/$segs*360;
$ar=$ang-$a0;
#print "$a0 + $ar\n";
$c0=cos($kk*$a0);
$s0=sin($kk*$a0);
$cf=cos($kk*($a0+360/$segs));
$sf=sin($kk*($a0+360/$segs));
$ct=cos($kk*$ang);
$st=sin($kk*$ang);
$scale=($c0*$sf-$s0*$cf)/(($sf-$s0)*$ct-($cf-$c0)*$st);
$reff=$scale*$rad;
$rieff=$scale*($rad-$thick);
#print "Effective radius $reff\n";
push @reffs, $reff;
push @rieffs, $rieff;
}
@pts=();
@ipts=();
foreach $ia (0..$#angs) {
$x=$reffs[$ia]*cos($kk*$angs[$ia]);
$z=$reffs[$ia]*sin($kk*$angs[$ia]);
push @pts, sprintf("0 // Point %2d: %.3f 0 %.3f\n",
$ia+1, $x, $z);
if ($thick>0) {
$x=$rieffs[$ia]*cos($kk*$angs[$ia]);
$z=$rieffs[$ia]*sin($kk*$angs[$ia]);
push @ipts, sprintf("0 // Inner point %2d: %.3f 0 %.3f\n",
$ia+1, $x, $z);
}
}
print @pts;
print @ipts;