Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use Math::Trig;
- $direct = 90;
- @curPos = (200,200);
- sub drawLine()
- {
- my(@straightLine, $lineBuffer) = @_;
- #print @straightLine;
- #x1 = straightLine[1], y1 = straightLine[2], x2 = straightLine[3], y2 = straightLine[4]
- #calculate distance from (x1,x2) to (y1,y2). d = sqrt(((x2 - x1)^2) + ((y2 - y1)^2) )
- $distance = sqrt((($straightLine[3] - $straightLine[1]) ** 2) + (($straightLine[4] - $straightLine[2]) ** 2));
- #calculate angle from (x1,x2) to (y1,y2). tan^(-1)(opp/adj) where opp = y2 - y1 and adj = x2 - x1
- #convert to radian for calculation
- $opp = deg2rad($straightLine[3] - $straightLine[1]);
- $adj = deg2rad($straightLine[4] - $straightLine[2]);
- #print "Straightline $straightLine[0] $straightLine[1] $straightLine[2] $straightLine[3] $straightLine[4]\n";
- #print "Opp: $opp. Adj: $adj\n";
- $angle = atan2($opp,$adj) + (2*pi);
- $angle = rad2deg($angle);
- #which direction to turn the turtle
- #if x2 = x1 and y2 = y1 then do nothing
- if($straightLine[3] == $straightLine[1] && $straightLine[4] == $straightLine[2])
- {
- return;
- }
- #if x2 = x1 and y2 > y1. go straight up
- elsif($straightLine[3] == $straightLine[1] && $straightLine[4] > $straightLine[2])
- {
- }
- #if x2 = x1 and y2 < y1. go down
- elsif($straightLine[3] == $straightLine[1] && $straightLine[4] < $straightLine[2])
- {
- $lineBuffer = $lineBuffer . "LEFT " . "180" . "\n";
- $direct += 180;
- $direct %= 360;
- }
- #if x2 < x1 and y2 = y1. go left
- elsif($straightLine[3] < $straightLine[1] && $straightLine[4] == $straightLine[2])
- {
- $lineBuffer = $lineBuffer . "LEFT " . "90" . "\n";
- $direct += 90;
- $direct %= 360;
- }
- #if x2 > x1 and y2 = y1. go right
- elsif($straightLine[3] > $straightLine[1] && $straightLine[4] == $straightLine[2])
- {
- $lineBuffer = $lineBuffer . "RIGHT " . "90" . "\n";
- $direct -= 90;
- $direct %= 360;
- }
- #if x2 < x1 and y2 > y1 then its in the second quad
- elsif($straightLine[3] < $straightLine[1] && $straightLine[4] > $straightLine[2])
- {
- $moveBy = 90 - $angle;
- $lineBuffer = $lineBuffer . "LEFT " . $moveBy . "\n";
- $direct += $angle;
- $direct %= 360;
- }
- #if x2 > x1 and y2 > y1 then its in the first quad
- elsif($straightLine[3] > $straightLine[1] && $straightLine[4] > $straightLine[2])
- {
- $moveBy = 90 - $angle;
- $lineBuffer = $lineBuffer . "RIGHT " . $moveBy . "\n";
- $direct -= $angle;
- $direct %= 360;
- }
- #if x2 < x1 and y2 < y1 then its in the third quad
- elsif($straightLine[3] < $straightLine[1] && $straightLine[4] < $straightLine[2])
- {
- $moveBy = 90 + $angle;
- $lineBuffer = $lineBuffer . "LEFT " . $moveBy . "\n";
- $direct += $moveBy;
- $direct %= 360;
- }
- #if x2 > x1 and y2 < y1 then its in the fourth quad
- elsif($straightLine[3] > $straightLine[1] && $straightLine[4] < $straightLine[2])
- {
- $moveBy = 90 + $angle;
- $lineBuffer = $lineBuffer . "RIGHT " . $moveBy . "\n";
- $direct -= $moveBy;
- $direct %= 360;
- }
- $lineBuffer = $lineBuffer . "FORWARD " . $distance . "\n";
- print $lineBuffer;
- #print "current x: $curPos[0]\n";
- #print "current y: $curPos[1]\n";
- $curPos[0] = $straightLine[3];
- $curPos[1] = $straightLine[4];
- }
- while(defined($line = <>))
- {
- #get rid of new line character
- chomp($line);
- #get rid of comments
- $line =~ s/#.*$//;
- #split the line by the spaces so each element is in the array
- my @current = split(/ /, $line);
- my @originToStart = ("Origin", $curPos[0], $curPos[1], $current[1], $current[2]);
- #print "Origin array: @originToStart\n";
- #get turtle to start point
- &drawLine(@originToStart, $lineBuffer);
- #choose which shape its trying to draw
- if($current[0] eq "LINE")
- {
- print "PENDOWN\n";
- #get turtle to end point
- &drawLine(@current, $lineBuffer);
- print "PENUP\n";
- $lineBuffer = "";
- }
- }
Add Comment
Please, Sign In to add comment