Skip to content

Instantly share code, notes, and snippets.

@oeloeloel
Created October 2, 2020 18:40
Show Gist options
  • Save oeloeloel/a5a22cc65f3a43d7b19a4c9674ef858a to your computer and use it in GitHub Desktop.
Save oeloeloel/a5a22cc65f3a43d7b19a4c9674ef858a to your computer and use it in GitHub Desktop.
Bresenham Circle in DragonRuby
# Bresenham circle for DragonRuby (basic)
RED = [255, 0, 0]
def put_pixel(args, x, y, c)
args.outputs.solids << [x, y, 1, 1, *c]
end
def drawCircle(args, xc, yc, x, y)
put_pixel(args, xc + x, yc + y, RED)
put_pixel(args, xc - x, yc + y, RED)
put_pixel(args, xc + x, yc - y, RED)
put_pixel(args, xc - x, yc - y, RED)
put_pixel(args, xc + y, yc + x, RED)
put_pixel(args, xc - y, yc + x, RED)
put_pixel(args, xc + y, yc - x, RED)
put_pixel(args, xc - y, yc - x, RED)
end
# circle-generation using Bresenham's algorithm
def circleBres(args, xc, yc, r)
x = 0
y = r
d = 3 - (2 * r)
drawCircle(args, xc, yc, x, y)
while y >= x
if d.positive?
y -= 1
d = d + 4 * (x - y) + 10
else
d = d + (4 * x) + 6
end
x += 1
drawCircle(args, xc, yc, x, y)
end
end
def tick(args)
xc = 640 # x centre
yc = 360 # y centre
r = 100 # radius
circleBres(args, xc, yc, r)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment