#!/usr/bin/perl
use strict;
use Math::Trig;
use Math::Trig qw(:radial);
use VRML;
use VRML::VRML2;
use subs qw(round);
use vars qw($precision);
die "Usage: perl gearmkr teeth indent% height" if($#ARGV<2);
my $cgi=new CGI;
my ($teeth,$indent,$h)=@ARGV;
my $precision=3;
my $vrml=new VRML(2);
$vrml->worldinfo('Gear Maker','Created by Mr. Guy Smiley');
$vrml->navigationinfo('EXAMINE');

my @coords=();
for(my $i=0;$i<2*$teeth;$i++) { 
	my $r=1;
	if($i%2!=0) { $r=1-$indent/100; }
	my $angle=deg2rad($i*180/$teeth);
	my ($x,$y,$z)=round(cylindrical_to_cartesian($r,$angle,0));
	push @coords, "$x $y";
	}
my @spine=("0 0 0","0 $h 0");

$vrml->begin;
$vrml->viewpoint('Above',"0 ".($h+5)." 0","1 0 0  -90");
$vrml->Shape(sub{$vrml->Extrusion(\@coords,\@spine,undef,undef,undef,undef,undef,undef,'FALSE','FALSE')},
sub{$vrml->appearance('d=gold')});
$vrml->end;

print $cgi->header(-type=>'x-world/x-vrml');
$vrml->print;

sub round { 
   my @array=();
   foreach (@_) {
	push(@array,int($_*10**$precision)/(10**$precision)); 
	}
   return @array;
   }