#!/usr/local/bin/perl

#    This is a sample c program that writes a VAL program.
#    It takes three command line arguments:
#
#    1. a radius
#    2. an angle
#    3. an integer
#
#    and outputs a VAL program that will move the robot in a circular
#    arc that encompasses the angle inputted.  The circle is actually
#    approximated by straight line segments, and the last input, the
#    integer, is how many line segments should be used to approximate
#    the circle.

#    For perl documentation, see http://www.perl.org

$r = $ARGV[0];
$theta = $ARGV[1]*3.1415926/180.0;
$count = $ARGV[2];

$x = 100;
$y = 250;
$z = -175;
$o = 179;
$a = 1;
$t = 89;

open OUT, "> program.txt";

$i=2;
for($ang=0.0;$ang<$theta;$ang+=$theta/$count) {
  $x -= ($r*$theta/$count*cos($ang));
  $y += ($r*$theta/$count*sin($ang));
  print OUT "DP P",$i++," $x $y $z $o $a $t\n";
}

print OUT "DP P1 100 250 -175 179 1 89\n";
print OUT "SPEED 10 ALWAYS\n";
print OUT "MOVE P1\n";

for($ang=0.0;$ang<$theta;$ang+=$theta/$count) {
  $dx = -$r*$theta/$count*cos($ang);
  $dy = $r*$theta/$count*sin($ang);
  # the next two lines prevent exponential notation
  if(abs($dx) < 0.01) { $dx = 0.0; }
  if(abs($dy) < 0.01) { $dy = 0.0; }
  print OUT "DRAW ",$dx,", ",$dy,"\n";
}

$i=2;
for($ang=0.0;$ang<$theta;$ang+=$theta/$count) {
  print OUT "MOVE P",$i++,"\n";
}

print OUT "READY\n";
