Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Adapted from https://iq.opengenus.org/bresenhams-circle-drawing-algorithm/ to lua
- -- going to use this for a few things soon:tm:
- local function putpixel(x, y, color)
- term.setCursorPos(x, y)
- term.setBackgroundColor(color)
- io.write(' ')
- end
- local function displayBresenhamCircle(xc_, yc_, x, y)
- putpixel(xc_+x, yc_+y, colors.white);
- putpixel(xc_-x, yc_+y, colors.white);
- putpixel(xc_+x, yc_-y, colors.white);
- putpixel(xc_-x, yc_-y, colors.white);
- putpixel(xc_+y, yc_+x, colors.white);
- putpixel(xc_-y, yc_+x, colors.white);
- putpixel(xc_+y, yc_-x, colors.white);
- putpixel(xc_-y, yc_-x, colors.white);
- end
- local function drawBresenhamCircle(radius_, xc, yc)
- term.clear()
- local x = 0
- local y = radius_
- local decisionParameter = 3 - 2 * radius_
- displayBresenhamCircle(xc, yc, x, y)
- while y >= x do
- x = x + 1
- if decisionParameter > 0 then
- y = y - 1
- decisionParameter = decisionParameter + 4 * (x - y) + 10
- else
- decisionParameter = decisionParameter + 4 * x + 6
- end
- displayBresenhamCircle(xc, yc, x, y)
- os.sleep(0.1)
- end
- end
- local function readNum(out)
- io.write(out .. ": ")
- local got = tonumber(io.read())
- return type(got) == "number" and got or readNum(out)
- end
- drawBresenhamCircle(readNum("Radius of circle"), readNum("X pos of circle"), readNum("Y pos of circle"))
- os.sleep(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement