Advertisement
klindley

ellipse

Mar 19th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. -- drawElipse <semi_major_axis> <semi_minor_axis>
  2. -- place turtle facing semi-minor axis in the center
  3. sMa, sma = {...}
  4. stage = 1
  5. running = true
  6. selectedItem = 1
  7.  
  8. --[[stages 1=started, moving toward sma
  9. 2=moving right, back
  10. 3=moving left, back
  11. 4=moving left, forward
  12. 5=moving right, forward]]--
  13. 1
  14. location = {x=0, y=0}
  15. foci = {1={x=0, y=0}, 2={x=0,y=0}}
  16.  
  17. function findFoci()
  18. dF = math.sqrt((sMa*sMa)-(sma*sma))
  19. foci[1].x = math.floor(dF)
  20. foci[2].x = -(math.floor(dF)
  21. end
  22.  
  23. function move(dir)
  24. -- dir = 1 forward, 2 = right, 3 = back, 4 = left
  25. -- note: turtle will ALWAYS face the same direction
  26. if dir == 1 then
  27. if turtle.forward() then
  28. location.y = location.y + 1
  29. end
  30. elsif dir == 2 then
  31. turtle.turnRight()
  32. if turtle.forward() then
  33. location.x = location.x + 1
  34. end
  35. turtle.turnLeft()
  36. elseif dir == 3 then
  37. if turtle.back() then
  38. location.y = location.y - 1
  39. end
  40. elseif dir == 4 then
  41. turtle.turnLeft()
  42. if turtle.forward() then
  43. location.x = location.x - 1
  44. end
  45. turtle.turnRight()
  46. end
  47.  
  48. end
  49.  
  50. function focalDistance()
  51. dx1 = location.x - foci[1].x
  52. dx2 = location.x - foci[2].x
  53. dy1 = location.y - foci[1].y
  54. dy2 = location.y - foci[2].y
  55.  
  56. df1 = math.sqrt(dx1*dx1 + dy1*dy1)
  57. df2 = math.sqrt(dx2*dx2 + dy2*dy2)
  58.  
  59. return math.floor(df1+df2)
  60. end
  61.  
  62. function chooseItem()
  63. if turtle.getItemCount(selectedItem) > 2 then
  64. selectedItem = selectedItem + 1
  65. end
  66. turtle.select(selectedItem)
  67. end
  68.  
  69. function main()
  70. chooseItem()
  71. if stage == 1 then
  72. if focalDistance() < (sMa * 2) then
  73. move(1)
  74. else
  75. turtle.placeUp()
  76. stage = 2
  77. return
  78. end
  79. elseif stage == 2 then
  80. move(2)
  81. if focalDistance() > (sMa * 2) then
  82. move(3)
  83. end
  84. turtle.placeUp()
  85.  
  86. if location.x == 0 then
  87. stage = 3
  88. return
  89. end
  90. elseif stage == 3 then
  91. move( 3)
  92. if focalDistance() > (2*sMa) then
  93. move(4)
  94. end
  95. turtle.placeUp()
  96.  
  97. if location.y == 0 then
  98. stage = 4
  99. return
  100. end
  101. elseif stage == 4 then
  102. move(4)
  103. if focalDistance() == (2*sMa) then
  104. move(1)
  105. end
  106. turtle.placeUp()
  107. if location.y == 0 then
  108. stage = 5
  109. return
  110. end
  111. elseif stage == 5
  112. move(1)
  113. if focalDistance() > (2*sMa) then
  114. move(2)
  115. end
  116. turtle.placeUp()
  117. if location.y == 0 then
  118. stage = 6 return
  119. end
  120. elseif stage == 6 then
  121. move(3)
  122. if location.x == 0 and location.y == 0 then
  123. running = false
  124. end
  125.  
  126. end
  127. end
  128.  
  129. while running do
  130. parallel.waitForAny(main)
  131. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement