Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 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 = 1 -- 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(d)
  54. x = x + d[1]
  55. y = y + d[2]
  56. end
  57.  
  58. while 1 do
  59. if x == radius and y == 1 then
  60. turtle.turnLeft()
  61. turtle.back()
  62. turtle.place()
  63. break
  64. end
  65.  
  66. turtle.place()
  67.  
  68. -- initial move
  69. move(forward[quarter])
  70.  
  71. if grid[x][y] == 0 then
  72. if (quarter == 0 and (y > 0 and grid[x][y-1] ~= 0)) or
  73. (quarter == 1 and (x < width-1 and grid[x+1][y] ~= 0)) or
  74. (quarter == 2 and (y < height-1 and grid[x][y+1] ~= 0)) or
  75. (quarter == 3 and (x > 0 and grid[x-1][y] ~= 0)) then
  76.  
  77. turtle.turnLeft()
  78. turtle.back()
  79. turtle.turnRight()
  80.  
  81. move(left[quarter])
  82.  
  83. elseif (quarter == 0 and grid[x][y+1] ~= 0) or
  84. (quarter == 1 and grid[x-1][y] ~= 0) or
  85. (quarter == 2 and grid[x][y-1] ~= 0) or
  86. (quarter == 3 and grid[x+1][y] ~= 0) then
  87.  
  88. turtle.turnRight()
  89. turtle.back()
  90. turtle.turnLeft()
  91.  
  92. move(right[quarter])
  93. end
  94. end
  95.  
  96. turtle.back()
  97.  
  98. if (quarter == 0 and width - x - 1 <= y) or
  99. (quarter == 1 and x <= y) or
  100. (quarter == 2 and x <= height - y - 1) or
  101. (quarter == 3 and x >= y) then
  102.  
  103. turtle.place()
  104. turtle.turnRight()
  105. turtle.back()
  106. turtle.back()
  107.  
  108. move(change[quarter])
  109. quarter = (quarter % 4) + 1
  110. end
  111. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement