68 lines
1.7 KiB
Plaintext
68 lines
1.7 KiB
Plaintext
T Colour = BVec3
|
||
|
||
V black = Colour(0, 0, 0)
|
||
V white = Colour(255, 255, 255)
|
||
|
||
T Bitmap
|
||
Int width, height
|
||
Colour background
|
||
[[Colour]] map
|
||
|
||
F (width = 40, height = 40, background = white)
|
||
assert(width > 0 & height > 0)
|
||
.width = width
|
||
.height = height
|
||
.background = background
|
||
.map = (0 .< height).map(h -> (0 .< @width).map(w -> @@background))
|
||
|
||
F fillrect(x, y, width, height, colour = black)
|
||
assert(x >= 0 & y >= 0 & width > 0 & height > 0)
|
||
L(h) 0 .< height
|
||
L(w) 0 .< width
|
||
.map[y + h][x + w] = colour
|
||
|
||
F chardisplay()
|
||
V txt = .map.map(row -> row.map(bit -> (I bit == @@.background {‘ ’} E ‘@’)).join(‘’))
|
||
txt = txt.map(row -> ‘|’row‘|’)
|
||
txt.insert(0, ‘+’(‘-’ * .width)‘+’)
|
||
txt.append(‘+’(‘-’ * .width)‘+’)
|
||
print(reversed(txt).join("\n"))
|
||
|
||
F set(x, y, colour = black)
|
||
.map[y][x] = colour
|
||
|
||
F get(x, y)
|
||
R .map[y][x]
|
||
|
||
F line(x0, y0, x1, y1)
|
||
‘Bresenham's line algorithm’
|
||
V dx = abs(x1 - x0)
|
||
V dy = abs(y1 - y0)
|
||
V (x, y) = (x0, y0)
|
||
V sx = I x0 > x1 {-1} E 1
|
||
V sy = I y0 > y1 {-1} E 1
|
||
I dx > dy
|
||
V err = dx / 2.0
|
||
L x != x1
|
||
.set(x, y)
|
||
err -= dy
|
||
I err < 0
|
||
y += sy
|
||
err += dx
|
||
x += sx
|
||
E
|
||
V err = dy / 2.0
|
||
L y != y1
|
||
.set(x, y)
|
||
err -= dx
|
||
I err < 0
|
||
x += sx
|
||
err += dy
|
||
y += sy
|
||
.set(x, y)
|
||
|
||
V bitmap = Bitmap(17, 17)
|
||
L(x0, y0, x1, y1) ((1, 8, 8, 16), (8, 16, 16, 8), (16, 8, 8, 1), (8, 1, 1, 8))
|
||
bitmap.line(x0, y0, x1, y1)
|
||
bitmap.chardisplay()
|