Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function place()
- while turtle.getItemCount() == 0 do
- turtle.select(turtle.getSelectedSlot() + 1)
- end
- turtle.place()
- end
- function createCircleGrid(radius)
- -- 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
- return grid
- end
- function buildCircle(radius)
- grid = createCircleGrid(radius)
- width = radius*2 + 1
- height = radius*2 + 1
- -- 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
- -- finish it off
- turtle.turnLeft()
- turtle.back()
- place()
- -- sit on top of the starting point
- turtle.up()
- turtle.forward()
- turtle.turnRight()
- turtle.back()
- turtle.back()
- break
- end
- 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) or
- (quarter == 4 and x >= y) then
- place()
- turtle.turnRight()
- turtle.back()
- turtle.back()
- move(change[quarter])
- quarter = (quarter % 4) + 1
- end
- end
- end
- function buildCylinder(radius, height)
- for i = 1, height do
- buildCircle(radius)
- end
- end
- function buildDome(radius)
- -- create grid for identifying radius of each dome layer
- grid = createCircleGrid(radius)
- prev_r = 0
- -- work our way up the dome
- for i = 1, radius do
- -- find radius at this point
- y = radius - i + 1
- x = 1
- while grid[x][y] == 0 do
- x = x + 1
- end
- r = radius - x + 1
- if prev_r > 0 then
- diff = prev_r - r
- if diff > 0 then
- turtle.turnRight()
- for j = 1, diff do
- turtle.back()
- end
- turtle.turnLeft()
- end
- end
- prev_r = r
- -- build circle of this radius
- buildCircle(r)
- end
- end
- buildDome(7)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement