39 lines
602 B
Plaintext
39 lines
602 B
Plaintext
drawLine = function(img, x0, y0, x1, y1, colr)
|
|
sign = function(a, b)
|
|
if a < b then return 1
|
|
return -1
|
|
end function
|
|
|
|
dx = abs(x1 - x0)
|
|
sx = sign(x0, x1)
|
|
|
|
dy = abs(y1 - y0)
|
|
sy = sign(y0, y1)
|
|
|
|
if dx > dy then
|
|
err = dx
|
|
else
|
|
err = -dy
|
|
end if
|
|
err = floor(err / 2)
|
|
|
|
while true
|
|
img.setPixel x0, y0, colr
|
|
if x0 == x1 and y0 == y1 then break
|
|
e2 = err
|
|
if e2 > -dx then
|
|
err -= dy
|
|
x0 += sx
|
|
end if
|
|
if e2 < dy then
|
|
err += dx
|
|
y0 += sy
|
|
end if
|
|
end while
|
|
end function
|
|
|
|
img= Image.create(320, 320)
|
|
drawLine img, 0, 0, 250, 300, color.red
|
|
gfx.clear
|
|
gfx.drawImage img, 0, 0
|