#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;

# Warning: this script is buggy because I am a terrible, terrible programmer. I should've just use
# a spreadsheet. It'll work for 4 equidistant support arms, 8 sometimes but it gives extra fractional
# lengths for some arms that aren't needed. The implementation is just stupid, but it was relatively
# quick to do.
# 
# Sorry.

my $inner_radius = 0.005;
#my $outer_radius = 0.35; # not used or needed anywhere
my $growth_rate = 0.03;
#my $arm_width = 0.005; # not used or needed anywhere
#my $gap_spacing = 0.01; # not used or needed anywhere
my $turns = 11.5;

my $fractional = 0;
$turns =~ /\.(\d+)/;
my $inverse_fractional;
if ($1) {
	$fractional = "0.$1";
	$inverse_fractional = 1/$fractional; 
}
my $number_support_arms = 4; # 2 crossed pvc pipes = 4 arms
my $increment = $growth_rate/$number_support_arms; 

GetOptions(
       'r1:s' => \$inner_radius,
       'a:s' => \$growth_rate,
       't:s' => \$turns,
       'arms:s' => \$number_support_arms, # 4 or 8	
#       'r2:s' => \$outer_radius, # not used or needed anywhere
#       'w:s' => \$arm_width, # not used or needed anywhere
#       's:s' => \$gap_spacing, # not used or needed anywhere
       'h:s' => \&help
);

sub help {
	print "\n-r1 = inner radius, -a = growth per full (2pi) turn, -t = full turns in multiples of 0.5";
	print "\n$0 r1 0.005 -a 0.03 -t 11.5";
	print "\n\n-arms = # of support arms, 4 or 8";
	print "\n$0 r1 0.005 -a 0.03 -t 11.5 -arms 8\n";
	exit;
}

# $inner_radius + ($increment * $arm) + ($turn * $growth_rate)

print "inner radius:$inner_radius \nturns: $turns \ngrowth rate: $growth_rate \nincrement/arm: $increment\n";
print "\nMeasurements are from arbitrary center point and all units are in meters.\n";


for my $arm (0 .. ($number_support_arms-1)) {
print "Arm " . ($arm+1) . "\n";

my $i = 0;
for my $i (0 .. $turns) {

	my $radius = ($inner_radius + ($increment * $arm)) + ($i * $growth_rate);
	print "\t$radius, $i\n";


	if ((($number_support_arms-$inverse_fractional) >= $arm) and $arm != 0) {
		#print $number_support_arms-$inverse_fractional;
		
		if ($i == ($turns-$fractional)) {
			print "*";
			my $last_increment = $inverse_fractional*$increment;
			$radius += $last_increment;
			print "\t$radius, " . ($i+$fractional) ."\n";	
			next;	
		}

		if (($arm == ($number_support_arms*$fractional) and $fractional)) {

			if ($i == ($turns-$fractional)) {
				print "*";
				my $last_increment = $inverse_fractional*$increment;
				$radius += $last_increment;
				print "\t$radius, " . ($i+$fractional) ."\n";		
			}
		}

	}	
}
}

