Bitmap/Bresenham's line algorithm: Difference between revisions

(→‎{{header|Raku}}: Fix up some internal links)
Line 2,285:
3 4</lang>
 
=={{header|KotlinLua}}==
{{trans|JavaC}}
{{works with|Lua 5.1 (or above, tested on: 5.1.5, 5.2.3, 5.3.5)}}
<lang scala>// version 1.1.2
<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 })
 
------------------------------
import java.awt.*
-- Bresenham's Line Algorithm:
import javax.swing.*
------------------------------
 
Bitmap.line = function(self, x1, y1, x2, y2, c)
class Bresenham(w: Int, h: Int) : JPanel() {
local dx, sx = math.abs(x2-x1), x1<x2 and 1 or -1
private val centerX = w / 2
local dy, sy = math.abs(y2-y1), y1<y2 and 1 or -1
private val centerY = h / 2
local err = math.floor((dx>dy and dx or -dy)/2)
 
while(true) do
init {
self:set(x1, y1, 0xFFFFFFFF)
preferredSize = Dimension(w, h)
if (x1==x2 and y1==y2) then
background = Color.blue
} break
end
 
local er2 = err
override fun paintComponent(g: Graphics) {
if (er2 > -dx) then
super.paintComponent(g)
drawLine(gerr, 0,x1 0,= 8err-dy, 19) // NNEx1+sx
end
drawLine(g, 0, 0, 19, 8) // ENE
if drawLine(g,er2 0,< 0, 19, -8dy) // ESEthen
drawLine(gerr, 0,y1 0,= 8err+dx, -19) // SSEy1+sy
end
drawLine(g, 0, 0, -8, -19) // SSW
end
drawLine(g, 0, 0, -19, -8) // WSW
end
drawLine(g, 0, 0, -19, 8) // WNW
drawLine(g, 0, 0, -8, 19) // NNW
}
 
private fun plot(g: Graphics, x: Int, y: Int) {
g.color = Color.white
g.drawOval(centerX + x * 10, centerY -y * 10, 10, 10)
}
 
private fun drawLine(g: Graphics, x1: Int, y1: Int, x2: Int, y2: Int) {
var d = 0
val dy = Math.abs(y2 - y1)
val dx = Math.abs(x2 - x1)
val dy2 = dy shl 1
val dx2 = dx shl 1
val ix = if (x1 < x2) 1 else -1
val iy = if (y1 < y2) 1 else -1
var xx = x1
var yy = y1
 
if (dy <= dx) {
while (true) {
plot(g, xx, yy)
if (xx == x2) break
xx += ix
d += dy2
if (d > dx) {
yy += iy
d -= dx2
}
}
}
else {
while (true) {
plot(g, xx, yy)
if (yy == y2) break
yy += iy
d += dx2
if (d > dy) {
xx += ix
d -= dy2
}
}
}
}
}
 
--------
fun main(args: Array<String>) {
-- Demo:
SwingUtilities.invokeLater {
--------
val f = JFrame()
Bitmap.render = function(self, charmap)
f.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
for y = 1, fself.isVisible =height truedo
local rowtab = {}
f.add(Bresenham(600, 500), BorderLayout.CENTER)
for x = 1, fself.title =width "Bresenham"do
rowtab[x] = charmap[self.pixels[y][x]]
f.isResizable = false
f.pack()end
print(table.concat(rowtab))
f.setLocationRelativeTo(null)
}end
end
}</lang>
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}}==
Anonymous user