Anonymous user
Bitmap/Bresenham's line algorithm: Difference between revisions
→{{header|Lua}}
Thundergnat (talk | contribs) (→{{header|Raku}}: Fix up some internal links) |
|||
Line 2,285:
3 4</lang>
=={{header|
{{trans|
{{works with|Lua 5.1 (or above, tested on: 5.1.5, 5.2.3, 5.3.5)}}
<lang Lua>
-----------------------------------------------
-- Bitmap replacement
-- (why? current Lua impl lacks a "set" method)
-----------------------------------------------
local Bitmap = {
new = function(self, width, height)
local instance = setmetatable({ width=width, height=height }, self)
instance:alloc()
return instance
end,
alloc = function(self)
self.pixels = {}
for y = 1, self.height do
self.pixels[y] = {}
for x = 1, self.width do
self.pixels[y][x] = 0x00000000
end
end
end,
clear = function(self, c)
for y = 1, self.height do
for x = 1, self.width do
self.pixels[y][x] = c or 0x00000000
end
end
end,
get = function(self, x, y)
x, y = x+1, y+1
if ((x>=1) and (x<=self.width) and (y>=1) and (y<=self.height)) then
return self.pixels[y][x]
else
return nil
end
end,
set = function(self, x, y, c)
x, y = x+1, y+1
if ((x>=1) and (x<=self.width) and (y>=1) and (y<=self.height)) then
self.pixels[y][x] = c or 0x00000000
end
end,
}
Bitmap.__index = Bitmap
setmetatable(Bitmap, { __call = function (t, ...) return t:new(...) end })
------------------------------
-- Bresenham's Line Algorithm:
------------------------------
Bitmap.line = function(self, x1, y1, x2, y2, c)
local dx, sx = math.abs(x2-x1), x1<x2 and 1 or -1
local dy, sy = math.abs(y2-y1), y1<y2 and 1 or -1
local err = math.floor((dx>dy and dx or -dy)/2)
while(true) do
self:set(x1, y1, 0xFFFFFFFF)
if (x1==x2 and y1==y2) then
end
local er2 = err
if (er2 > -dx) then
end
if
end
end
end
--------
-- Demo:
--------
Bitmap.render = function(self, charmap)
for y = 1,
local rowtab = {}
for x = 1,
rowtab[x] = charmap[self.pixels[y][x]]
print(table.concat(rowtab))
end
local bitmap = Bitmap(61,21)
bitmap:clear()
bitmap:line(0,10,30,0)
bitmap:line(30,0,60,10)
bitmap:line(60,10,30,20)
bitmap:line(30,20,0,10)
bitmap:render({[0x000000]='.', [0xFFFFFFFF]='X'})</lang>
{{out}}<pre>.............................XXX.............................
..........................XXX...XXX..........................
.......................XXX.........XXX.......................
....................XXX...............XXX....................
.................XXX.....................XXX.................
..............XXX...........................XXX..............
...........XXX.................................XXX...........
........XXX.......................................XXX........
.....XXX.............................................XXX.....
..XXX...................................................XXX..
XX.........................................................XX
..XXX...................................................XXX..
.....XXX.............................................XXX.....
........XXX.......................................XXX........
...........XXX.................................XXX...........
..............XXX...........................XXX..............
.................XXX.....................XXX.................
....................XXX...............XXX....................
.......................XXX.........XXX.......................
..........................XXX...XXX..........................
.............................XXX.............................
</pre>
=={{header|Maple}}==
|