-- circle.lua — CC:Tweaked Turtle
-- Moves up 5, then draws a 2D circle outline on the floor below.
-- Default radius: 50
local radius = 50
-- ==== helpers ====
local function forward()
while not turtle.forward() do
if turtle.detect() then turtle.dig() end
end
end
local function up()
while not turtle.up() do
if turtle.detectUp() then turtle.digUp() end
end
end
local function placeDown()
for s=1,16 do
if turtle.getItemCount(s) > 0 then
turtle.select(s)
turtle.placeDown()
return true
end
end
return false
end
-- ==== movement tracking ====
local dir = 0 -- 0=north,1=east,2=south,3=west
local posX,posZ = 0,0
local function turnRight()
turtle.turnRight()
dir = (dir+1)%4
end
local function turnLeft()
turtle.turnLeft()
dir = (dir+3)%4
end
local function step()
forward()
if dir==0 then posZ=posZ-1
elseif dir==1 then posX=posX+1
elseif dir==2 then posZ=posZ+1
elseif dir==3 then posX=posX-1 end
end
-- teleport-style move (grid walking)
local function goto(x,z)
-- move in X
if posX < x then
while dir~=1 do turnRight() end
for i=posX+1,x do step() end
elseif posX > x then
while dir~=3 do turnRight() end
for i=x,posX-1 do step() end
end
-- move in Z
if posZ < z then
while dir~=2 do turnRight() end
for i=posZ+1,z do step() end
elseif posZ > z then
while dir~=0 do turnRight() end
for i=z,posZ-1 do step() end
end
end
-- ==== main ====
-- go up 5
for i=1,5 do up() end
-- draw circle with midpoint algorithm
local function drawCircle(r)
local x = r
local z = 0
local err = 1 - r
while x >= z do
local pts = {
{ x, z }, { z, x }, { -z, x }, { -x, z },
{ -x, -z }, { -z, -x }, { z, -x }, { x, -z }
}
for _,p in ipairs(pts) do
goto(p[1],p[2])
placeDown()
end
z = z + 1
if err < 0 then
err = err + 2*z + 1
else
x = x - 1
err = err + 2*(z - x + 1)
end
end
end
drawCircle(radius)
print("Circle complete!")