RosettaCodeData/Task/Bitmap-Bresenhams-line-algorithm/Pluto/bitmap-bresenhams-line-algorithm-1.pluto
2026-04-30 12:34:36 -04:00

27 lines
749 B
Text

local canvas = require "canvas"
local function line(c, x0, y0, x1, y1, col)
local dx = math.abs(x1 - x0)
local dy = math.abs(y1 - y0)
local sx = (x0 < x1) ? 1 : -1
local sy = (y0 < y1) ? 1 : -1
local err = (dx > dy ? dx : - dy) // 2
while true do
c:set(x0, y0, col)
if x0 == x1 and y0 == y1 then break end
local e2 = err
if e2 > -dx then
err -= dy
x0 += sx
end
if e2 < dy then
err += dx
y0 += sy
end
end
end
local c = canvas.new(400, 400)
c:fill(0xffffff) -- white background
line(c, 0, 0, 399, 399, 0x0000ff) -- draw a red diagonal
io.contents("mybmp.bmp", c:tobmp()) -- save to a bitmap file