Advertisement
klindley

elipsev2

Mar 19th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.61 KB | None | 0 0
  1. -- drawElipse <semi_major_axis> <semi_minor_axis>
  2. -- place turtle facing semi-minor axis in the center
  3. args = {...}
  4. sMa = args[1]
  5. sma = args[2]
  6.  
  7. print(sMa)
  8. print(sma)
  9.  
  10. stage = 1
  11. running = true
  12. selectedItem = 1
  13.  
  14. --[[stages 1=started, moving toward sma
  15.            2=moving right, back
  16.            3=moving left, back
  17.            4=moving left, forward
  18.            5=moving right, forward]]--
  19.            
  20. location = {x=0, y=0}
  21. foci = {{x=0, y=0},{x=0,y=0}}
  22.  
  23. function findFoci()
  24.     dF = math.sqrt((sMa*sMa)-(sma*sma))
  25.     foci[1].x = math.floor(dF)
  26.     foci[2].x = -(math.floor(dF))
  27. end
  28.  
  29. function move(dir)
  30. -- dir = 1 forward, 2 = forward/right, 3 = right, 4 = back/right, 5 = back, 6 = back/left, 7 = left, 8 = forward/left
  31. -- note: turtle will ALWAYS face the same direction
  32.     if dir == 1 then
  33.         if turtle.forward() then
  34.           location.y = location.y + 1
  35.         end
  36.     elseif dir == 2 then
  37.         move(1)
  38.         move(3)
  39.     elseif dir == 3 then
  40.       turtle.turnRight()
  41.       if turtle.forward() then
  42.         location.x = location.x + 1
  43.       end
  44.       turtle.turnLeft()
  45.     elseif dir == 4 then
  46.         move(5)
  47.         move(3)
  48.     elseif dir == 5 then
  49.         if turtle.back() then
  50.             location.y = location.y - 1
  51.         end
  52.     elseif dir == 6 then
  53.         move(5)
  54.         move(7)
  55.        
  56.     elseif dir == 7 then
  57.         turtle.turnLeft()
  58.             if turtle.forward() then
  59.                 location.x = location.x - 1
  60.             end
  61.         turtle.turnRight()
  62.     elseif dir == 8 then
  63.         move(1)
  64.         move(7)
  65.     end
  66.    
  67. end
  68.  
  69. function focalDistance(pos)
  70.   dx1 = pos.x - foci[1].x
  71.   dx2 = pos.x - foci[2].x
  72.   dy1 = pos.y - foci[1].y
  73.   dy2 = pos.y - foci[2].y
  74.  
  75.   print("Foci:"..textutils.serialize(foci))
  76.   print("Pos:"..textutils.serialize(pos))
  77.  
  78.   df1 = math.sqrt(dx1*dx1 + dy1*dy1)
  79.   df2 = math.sqrt(dx2*dx2 + dy2*dy2)
  80.  
  81.   return math.floor(df1+df2)
  82. end
  83.  
  84. function chooseItem()
  85.   if turtle.getItemCount(selectedItem) < 2 then
  86.     selectedItem = selectedItem + 1
  87.   end
  88.   turtle.select(selectedItem)
  89. end
  90.  
  91. function main()
  92.     chooseItem()
  93.     pos = {}
  94.     print("Stage: "..tostring(stage))
  95.     if stage == 1 then
  96.         move(1)
  97.         print("location: " ..textutils.serialize(location))
  98.         print("Focal Dist: "..tostring(math.floor(focalDistance(location))))
  99.         print("Major Axis: "..tostring(tonumber(sMa)*2))
  100.         if math.floor(focalDistance(location)) == tonumber(sMa)*2 then
  101.             turtle.placeUp()
  102.             stage = 2
  103.             return
  104.         end
  105.     elseif stage == 2 then
  106.         pos.x = location.x + 1
  107.         pos.y = location.y
  108.         if math.floor(focalDistance(pos)) == tonumber(sMa)*2 then
  109.             dir = 3
  110.         else
  111.             pos.x = location.x + 1
  112.             pos.y = location.y - 1
  113.             if math.floor(focalDistance(pos)) == tonumber(sMa)*2 then
  114.                 dir = 4
  115.             else
  116.                 pos.x = location.x
  117.                 pos.y = location.y - 1
  118.                 if math.floor(focalDistance(pos)) == tonumber(sMa)*2 then
  119.                     dir = 5
  120.                 end
  121.             end
  122.         end
  123.         move(dir)
  124.         turtle.placeUp()
  125.         if location.y == 0 then
  126.             stage = 3
  127.             return
  128.         end
  129.        
  130.     elseif stage == 3 then
  131.         pos.x = location.x
  132.         pos.y = location.y - 1
  133.         if math.floor(focalDistance(pos)) == tonumber(sMa)*2 then
  134.             dir = 5
  135.         else
  136.             pos.x = location.x - 1
  137.             pos.y = location.y - 1
  138.             if math.floor(focalDistance(pos)) == tonumber(sMa)*2 then
  139.                 dir = 6
  140.             else
  141.                 pos.x = location.x - 1
  142.                 pos.y = location.y
  143.                 if math.floor(focalDistance(pos)) == tonumber(sMa)*2 then
  144.                     dir = 7
  145.                 end
  146.             end
  147.         end
  148.         move(dir)
  149.         turtle.placeUp()
  150.         if location.x == 0 then
  151.             stage = 4
  152.             return
  153.         end
  154.                
  155.     elseif stage == 4 then
  156.         pos.x = location.x - 1
  157.         pos.y = location.y
  158.         if math.floor(focalDistance(pos)) == tonumber(sMa)*2 then
  159.             dir = 7
  160.         else
  161.             pos.x = location.x - 1
  162.             pos.y = location.y + 1
  163.             if math.floor(focalDistance(pos)) == tonumber(sMa)*2 then
  164.                 dir = 8
  165.             else
  166.                 pos.x = location.x
  167.                 pos.y = location.y + 1
  168.                 if math.floor(focalDistance(pos)) == tonumber(sMa)*2 then
  169.                     dir = 1
  170.                 end
  171.             end
  172.         end
  173.         move(dir)
  174.         turtle.placeUp()
  175.         if location.y == 0 then
  176.             stage = 5
  177.             return
  178.         end
  179.     elseif stage == 5 then
  180.         pos.x = location.x
  181.         pos.y = location.y + 1
  182.         if math.floor(focalDistance(pos)) == tonumber(sMa)*2 then
  183.             dir = 1
  184.         else
  185.             pos.x = location.x + 1
  186.             pos.y = location.y + 1
  187.             if math.floor(focalDistance(pos)) == tonumber(sMa)*2 then
  188.                 dir = 2
  189.             else
  190.                 pos.x = location.x + 1
  191.                 pos.y = location.y
  192.                 if math.floor(focalDistance(pos)) == tonumber(sMa)*2 then
  193.                     dir = 3
  194.                 end
  195.             end
  196.         end
  197.         move(dir)
  198.         turtle.placeUp()
  199.         if location.x == 0 then
  200.             stage = 6
  201.             return
  202.         end
  203.  
  204.     elseif stage == 6 then
  205.         move(5)
  206.         if location.x == 0 and location.y == 0 then
  207.             running = false
  208.         end
  209.     end
  210. end
  211.  
  212. findFoci()
  213. while running do
  214.   parallel.waitForAny(main)
  215. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement