Advertisement
Guest User

circle.lua

a guest
Sep 21st, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.64 KB | None | 0 0
  1. print(turtle.refuel())
  2.  
  3. maxy = 1
  4. radius = 3
  5. intRadius = math.floor(radius)
  6. n = 2*(intRadius+1) + 1
  7. pixels = {}
  8.  
  9. function init()
  10.     turtle.up()
  11.     for i=1,intRadius+1 do
  12.         turtle.forward()
  13.     end
  14.     turtle.turnLeft()        
  15. end
  16.  
  17. init()
  18.  
  19. function setpixel(x, y)
  20.     local tx = math.floor(n/2)+x
  21.     local ty = math.floor(n/2)+y
  22.        
  23.     pixels[ty+1][tx+1] = 'x'
  24. end
  25.  
  26. for y=1,n do
  27.     pixels[y] = {}
  28.     for x=1,n do
  29.         pixels[y][x] = '.'
  30.     end
  31. end
  32.  
  33. local l = math.floor(radius * math.cos(math.pi/4))
  34.  
  35. for x=0,l do
  36.     y = math.floor(math.sqrt(radius*radius - x*x))
  37.     setpixel(x, y)
  38.     setpixel(-x, y)
  39.     setpixel(x, -y)
  40.     setpixel(-x, -y)
  41.     setpixel(y, x)
  42.     setpixel(-y, x)
  43.     setpixel(y, -x)
  44.     setpixel(-y, -x)
  45. end
  46.  
  47. for y=1,#pixels do
  48.     for x=1,#pixels do
  49.         write(pixels[y][x])
  50.     end
  51.     print()
  52. end
  53.  
  54. posx, posy = intRadius+2, n
  55. vx, vy = -1, 0
  56.  
  57. function left()
  58.     return vy*-1, vx
  59. end
  60.  
  61. function right()
  62.     return vy, vx*-1
  63. end
  64.  
  65. function turnLeft()
  66.     vx, vy = left()
  67.     turtle.turnLeft()
  68. end
  69.  
  70. function turnRight()
  71.     vx, vy = right()
  72.     turtle.turnRight()
  73. end
  74.  
  75. function pixelLeft()
  76.     vvx, vvy = left()
  77.     xx, yy = posx+vvx, posy+vvy
  78.     print('checking left: ', xx, yy)
  79.     if (xx < 1) or (xx >= n) then
  80.         return false
  81.     end
  82.     if (yy < 1) or (yy >= n) then
  83.         return false
  84.     end
  85.     return pixels[yy][xx] == 'x'
  86. end
  87.  
  88. function pixelForward()
  89.     xx, yy = posx+vx, posy+vy
  90.     if (xx < 1) or (xx >= n) then
  91.         return false
  92.     end
  93.     if (yy < 1) or (yy >= n) then
  94.         return false
  95.     end
  96.     return pixels[yy][xx] == 'x'
  97. end
  98.  
  99. function currentSlotEmpty()
  100.     return turtle.getItemCount(turtle.getSelectedSlot())==0
  101. end
  102.  
  103. function reSelect()
  104.     for i=1,16 do
  105.         if turtle.getItemCount(i) > 0 then
  106.             turtle.select(i)
  107.             return
  108.         end
  109.     end
  110.     -- exit the program early
  111.     error()
  112. end
  113.  
  114. function moveAndPlace()
  115.     posx, posy = posx+vx, posy+vy
  116.     turtle.forward()
  117.     if currentSlotEmpty() then
  118.         reSelect()
  119.     end
  120.     turtle.placeDown()
  121. end
  122.  
  123. function mainLoop()
  124.     print(posx, posy)
  125.     if pixelLeft() then
  126.         print('pixel left!')
  127.         if pixelForward() then
  128.             print('pixel forward!')
  129.             turnRight()
  130.         end
  131.     else
  132.         turnLeft()
  133.     end
  134.     moveAndPlace()
  135. end
  136.  
  137. currenty = 0
  138. repeat
  139.     -- one circle in xz plane
  140.     repeat
  141.         mainLoop()
  142.     until (posx==intRadius+2) and (posy==n)
  143.     currenty = currenty+1
  144.     turtle.up()
  145.     print('one more layer finished!')
  146. until currenty==maxy
  147. print('done! :)')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement