Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- radius = 10
- -- create the pixel array
- width = radius*2 + 1
- height = radius*2 + 1
- grid = {}
- for i = 1, width do
- grid[i] = {}
- for j = 1, height do
- grid[i][j] = 0
- end
- end
- -- draw circle into grid
- x0 = radius
- y0 = radius
- x = radius
- y = 0
- err = 1-x
- while x >= y do
- grid[x+x0+1][y+y0+1] = 1
- grid[y+x0+1][x+y0+1] = 1
- grid[-x+x0+1][y+y0+1] = 1
- grid[-y+x0+1][x+y0+1] = 1
- grid[-x+x0+1][-y+y0+1] = 1
- grid[-y+x0+1][-x+y0+1] = 1
- grid[x+x0+1][-y+y0+1] = 1
- grid[y+x0+1][-x+y0+1] = 1
- y = y + 1
- if err < 0 then
- err = err + 2*y + 1
- else
- x = x - 1
- err = err + 2*(y-x+1)
- end
- end
- -- build turtle steps (1-indexed x, y from here on)
- x = radius+1
- y = 1
- quarter = 1 -- top quarter, going around clockwise
- forward = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}
- left = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}
- right = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}}
- change = {{1, 1}, {-1, 1}, {-1, -1}, {1, -1}}
- function move(d)
- x = x + d[1]
- y = y + d[2]
- end
- while 1 do
- if x == radius and y == 1 then
- turtle.turnLeft()
- turtle.back()
- turtle.place()
- break
- end
- turtle.place()
- -- initial move
- move(forward[quarter])
- if grid[x][y] == 0 then
- if (quarter == 1 and (y > 1 and grid[x][y-1] ~= 0)) or
- (quarter == 2 and (x < width and grid[x+1][y] ~= 0)) or
- (quarter == 3 and (y < height and grid[x][y+1] ~= 0)) or
- (quarter == 4 and (x > 1 and grid[x-1][y] ~= 0)) then
- turtle.turnLeft()
- turtle.back()
- turtle.turnRight()
- move(left[quarter])
- elseif (quarter == 1 and grid[x][y+1] ~= 0) or
- (quarter == 2 and grid[x-1][y] ~= 0) or
- (quarter == 3 and grid[x][y-1] ~= 0) or
- (quarter == 4 and grid[x+1][y] ~= 0) then
- turtle.turnRight()
- turtle.back()
- turtle.turnLeft()
- move(right[quarter])
- end
- end
- turtle.back()
- if (quarter == 1 and width - x - 1 <= y) or
- (quarter == 2 and x <= y) or
- (quarter == 3 and x <= height - y - 1) or
- (quarter == 4 and x >= y) then
- turtle.place()
- turtle.turnRight()
- turtle.back()
- turtle.back()
- move(change[quarter])
- quarter = (quarter % 4) + 1
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement