RosettaCodeData/Task/Bitmap-Midpoint-circle-algo.../Pluto/bitmap-midpoint-circle-algo...

34 lines
897 B
Plaintext

local canvas = require "canvas"
local function trunc(x) return x >= 0 ? math.floor(x) : math.ceil(x) end
local function circle(c, cx, cy, r, col)
local d = trunc((5 - r * 4) / 4)
local x = 0
local y = r
repeat
c:set(cx + x, cy + y, col)
c:set(cx + x, cy - y, col)
c:set(cx - x, cy + y, col)
c:set(cx - x, cy - y, col)
c:set(cx + y, cy + x, col)
c:set(cx + y, cy - x, col)
c:set(cx - y, cy + x, col)
c:set(cx - y, cy - x, col)
if d < 0 then
d += 2 * x + 1
else
d += 2 * (x - y) + 1
y -= 1
end
++x
until x > y
end
local c = canvas.new(400, 400)
c:fill(0xffffff) -- white background
-- Draw a blue circle with centre at (200, 200) and radius 100.
circle(c, 200, 200, 100, 0xff0000)
io.contents("mybmp.bmp", c:tobmp()) -- save to a bitmap file