Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.44 KB | None | 0 0
  1. radius = 10
  2.  
  3. -- create the pixel array
  4. width = radius*2 + 1
  5. height = radius*2 + 1
  6. grid = {}
  7.  
  8. for i = 1, width do
  9.     grid[i] = {}
  10.  
  11.     for j = 1, height do
  12.         grid[i][j] = 0
  13.     end
  14. end
  15.  
  16. -- draw circle into grid
  17. x0 = radius
  18. y0 = radius
  19.  
  20. x = radius
  21. y = 0
  22. err = 1-x
  23.  
  24. while x >= y do
  25.     grid[x+x0+1][y+y0+1] = 1
  26.     grid[y+x0+1][x+y0+1] = 1
  27.     grid[-x+x0+1][y+y0+1] = 1
  28.     grid[-y+x0+1][x+y0+1] = 1
  29.     grid[-x+x0+1][-y+y0+1] = 1
  30.     grid[-y+x0+1][-x+y0+1] = 1
  31.     grid[x+x0+1][-y+y0+1] = 1
  32.     grid[y+x0+1][-x+y0+1] = 1
  33.     y = y + 1
  34.  
  35.     if err < 0 then
  36.         err = err + 2*y + 1
  37.     else
  38.         x = x - 1
  39.         err = err + 2*(y-x+1)
  40.     end
  41. end
  42.  
  43. -- build turtle steps (1-indexed x, y from here on)
  44. x = radius+1
  45. y = 1
  46. quarter = 0 -- top quarter, going around clockwise
  47.  
  48. forward = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}
  49. left = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}
  50. right = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}}
  51. change = {{1, 1}, {-1, 1}, {-1, -1}, {1, -1}}
  52.  
  53. function move(t)
  54.     dx, dy = t
  55.     x = x + dx
  56.     y = y + dy
  57. end
  58.  
  59. while 1 do
  60.     if x == radius and y == 1 then
  61.         turtle.turnLeft()
  62.         turtle.back()
  63.         turtle.place()
  64.         break
  65.     end
  66.  
  67.     turtle.place()
  68.  
  69.     -- initial move
  70.     move(forward[quarter])
  71.  
  72.     if grid[x][y] == 0 then
  73.         if (quarter == 0 and (y > 0 and grid[x][y-1] ~= 0)) or
  74.             (quarter == 1 and (x < width-1 and grid[x+1][y] ~= 0)) or
  75.             (quarter == 2 and (y < height-1 and grid[x][y+1] ~= 0)) or
  76.             (quarter == 3 and (x > 0 and grid[x-1][y] ~= 0)) then
  77.  
  78.             turtle.turnLeft()
  79.             turtle.back()
  80.             turtle.turnRight()
  81.            
  82.             move(left[quarter])
  83.  
  84.         elseif (quarter == 0 and grid[x][y+1] ~= 0) or
  85.             (quarter == 1 and grid[x-1][y] ~= 0) or
  86.             (quarter == 2 and grid[x][y-1] ~= 0) or
  87.             (quarter == 3 and grid[x+1][y] ~= 0) then
  88.  
  89.             turtle.turnRight()
  90.             turtle.back()
  91.             turtle.turnLeft()
  92.  
  93.             move(right[quarter])
  94.         end
  95.     end
  96.  
  97.     turtle.back()
  98.  
  99.     if (quarter == 0 and width - x - 1 <= y) or
  100.         (quarter == 1 and x <= y) or
  101.         (quarter == 2 and x <= height - y - 1) or
  102.         (quarter == 3 and x >= y) then
  103.  
  104.         turtle.place()
  105.         turtle.turnRight()
  106.         turtle.back()
  107.         turtle.back()
  108.  
  109.         move(change[quarter])
  110.         quarter = (quarter + 1) % 4
  111.     end
  112. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement