Guest User

Untitled

a guest
Aug 14th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.84 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use Math::Trig;
  4. $direct = 90;
  5. @curPos = (200,200);
  6.  
  7. sub drawLine()
  8. {  
  9.     my(@straightLine, $lineBuffer) = @_;
  10.    
  11.     #print @straightLine;
  12.     #x1 = straightLine[1], y1 = straightLine[2], x2 = straightLine[3], y2 = straightLine[4]
  13.     #calculate distance from (x1,x2) to (y1,y2). d = sqrt(((x2 - x1)^2) + ((y2 - y1)^2) )
  14.     $distance = sqrt((($straightLine[3] - $straightLine[1]) ** 2) + (($straightLine[4] - $straightLine[2]) ** 2));
  15.    
  16.        
  17.     #calculate angle from (x1,x2) to (y1,y2). tan^(-1)(opp/adj) where opp = y2 - y1 and adj = x2 - x1
  18.     #convert to radian for calculation
  19.     $opp = deg2rad($straightLine[3] - $straightLine[1]);
  20.     $adj = deg2rad($straightLine[4] - $straightLine[2]);
  21.     #print "Straightline $straightLine[0] $straightLine[1] $straightLine[2] $straightLine[3] $straightLine[4]\n";
  22.     #print "Opp: $opp. Adj: $adj\n";
  23.     $angle = atan2($opp,$adj) + (2*pi);
  24.     $angle = rad2deg($angle);
  25.    
  26.     #which direction to turn the turtle
  27.     #if x2 = x1 and y2 = y1 then do nothing
  28.     if($straightLine[3] == $straightLine[1] && $straightLine[4] == $straightLine[2])
  29.     {
  30.         return;
  31.     }
  32.     #if x2 = x1 and y2 > y1. go straight up
  33.     elsif($straightLine[3] == $straightLine[1] && $straightLine[4] > $straightLine[2])
  34.     {
  35.     }
  36.     #if x2 = x1 and y2 < y1. go down
  37.     elsif($straightLine[3] == $straightLine[1] && $straightLine[4] < $straightLine[2])
  38.     {
  39.         $lineBuffer = $lineBuffer . "LEFT " . "180" . "\n";
  40.         $direct += 180;
  41.                 $direct %= 360;
  42.     }
  43.     #if x2 < x1 and y2 = y1. go left
  44.     elsif($straightLine[3] < $straightLine[1] && $straightLine[4] == $straightLine[2])
  45.     {
  46.         $lineBuffer = $lineBuffer . "LEFT " . "90" . "\n";
  47.         $direct += 90;
  48.                 $direct %= 360;
  49.     }
  50.     #if x2 > x1 and y2 = y1. go right
  51.     elsif($straightLine[3] > $straightLine[1] && $straightLine[4] == $straightLine[2])
  52.     {
  53.         $lineBuffer = $lineBuffer . "RIGHT " . "90" . "\n";
  54.         $direct -= 90;
  55.                 $direct %= 360;
  56.     }
  57.     #if x2 < x1 and y2 > y1 then its in the second quad
  58.     elsif($straightLine[3] < $straightLine[1] && $straightLine[4] > $straightLine[2])  
  59.     {  
  60.         $moveBy = 90 - $angle;
  61.         $lineBuffer = $lineBuffer . "LEFT " . $moveBy . "\n";
  62.         $direct += $angle;
  63.         $direct %= 360;
  64.     }
  65.     #if x2 > x1 and y2 > y1 then its in the first quad
  66.     elsif($straightLine[3] >  $straightLine[1] && $straightLine[4] > $straightLine[2])
  67.     {
  68.         $moveBy = 90 - $angle;
  69.         $lineBuffer = $lineBuffer . "RIGHT " . $moveBy . "\n";
  70.         $direct -= $angle;
  71.                 $direct %= 360;
  72.     }
  73.     #if x2 < x1 and y2 < y1 then its in the third quad
  74.     elsif($straightLine[3] < $straightLine[1] && $straightLine[4] < $straightLine[2])
  75.     {
  76.         $moveBy = 90 + $angle;
  77.         $lineBuffer = $lineBuffer . "LEFT " . $moveBy . "\n";
  78.         $direct += $moveBy;
  79.                 $direct %= 360;
  80.     }
  81.     #if x2 > x1 and y2 < y1 then its in the fourth quad
  82.     elsif($straightLine[3] > $straightLine[1] && $straightLine[4] < $straightLine[2])
  83.     {
  84.         $moveBy = 90 + $angle;
  85.         $lineBuffer = $lineBuffer . "RIGHT " . $moveBy . "\n";
  86.         $direct -= $moveBy;
  87.         $direct %= 360;
  88.     }
  89.    
  90.     $lineBuffer = $lineBuffer . "FORWARD " . $distance . "\n";
  91.     print $lineBuffer;
  92.     #print "current x: $curPos[0]\n";
  93.     #print "current y: $curPos[1]\n";          
  94.     $curPos[0] = $straightLine[3];
  95.     $curPos[1] = $straightLine[4];
  96. }  
  97.  
  98. while(defined($line = <>))
  99. {
  100.     #get rid of new line character
  101.     chomp($line);
  102.    
  103.     #get rid of comments
  104.     $line =~ s/#.*$//;
  105.  
  106.     #split the line by the spaces so each element is in the array
  107.     my @current = split(/ /, $line);   
  108.     my @originToStart = ("Origin", $curPos[0], $curPos[1], $current[1], $current[2]);
  109.     #print "Origin array: @originToStart\n";
  110.    
  111.     #get turtle to start point
  112.     &drawLine(@originToStart, $lineBuffer);
  113.     #choose which shape its trying to draw
  114.     if($current[0] eq "LINE")
  115.     {  
  116.         print "PENDOWN\n";
  117.         #get turtle to end point
  118.         &drawLine(@current, $lineBuffer);      
  119.         print "PENUP\n";
  120.         $lineBuffer = "";  
  121.     }
  122.  
  123. }
Add Comment
Please, Sign In to add comment