Skip to content

Instantly share code, notes, and snippets.

@Xrayez
Last active April 16, 2023 13:41
Show Gist options
  • Save Xrayez/c7497990018a69bb4639e250d166a7cc to your computer and use it in GitHub Desktop.
Save Xrayez/c7497990018a69bb4639e250d166a7cc to your computer and use it in GitHub Desktop.
Basic Bresenham circle implementation in Lua
# Ported from https://www.daniweb.com/programming/software-development/threads/321181/python-bresenham-circle-arc-algorithm
-- Bresenham circle
function circle(radius)
local pixels = {}
local switch = 3 - (2 * radius)
local x = 0
local y = radius
while x <= y do
table.insert(pixels, {x, -y})
table.insert(pixels, {y, -x})
table.insert(pixels, {y, x})
table.insert(pixels, {x, y})
table.insert(pixels, {-x, y})
table.insert(pixels, {-y, x})
table.insert(pixels, {-y, -x})
table.insert(pixels, {-x, -y})
if switch < 0 then
switch = switch + (4 * x) + 6
else
switch = switch + (4 * (x - y)) + 10
y = y - 1
end
x = x + 1
end
return pixels
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment