Last active
April 16, 2023 13:41
-
-
Save Xrayez/c7497990018a69bb4639e250d166a7cc to your computer and use it in GitHub Desktop.
Basic Bresenham circle implementation in Lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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