Task

Using the data storage type defined on the Bitmap page for raster graphics images,

draw a line given two points with Bresenham's line algorithm.




' version 16-09-2015
' compile with: fbc -s console
' OR compile with: fbc -s gui
 
' Ported from the C version
Sub Br_line(x0 As Integer, y0 As Integer, x1 As Integer, y1 As Integer, Col As Integer = &HFFFFFF)
 
    Dim As Integer dx = Abs(x1 - x0), dy = Abs(y1 - y0)
    Dim As Integer sx = IIf(x0 < x1, 1, -1)
    Dim As Integer sy = IIf(y0 < y1, 1, -1)
    Dim As Integer er = IIf(dx > dy, dx, -dy) \ 2, e2
 
    Do
        PSet(x0, y0), col
        If (x0 = x1) And (y0 = y1) Then Exit Do
        e2 = er
        If e2 > -dx Then Er -= dy : x0 += sx
        If e2 <  dy Then Er += dx : y0 += sy
    Loop
 
End Sub
 
' ------=< MAIN >=------
 
Dim As Double x0, y0, x1, y1
 
ScreenRes 400, 400, 32
WindowTitle" Press key to end demo"
Randomize Timer
 
Do
    Cls
    For a As Integer = 1 To 20
        Br_line(Rnd*380+10, Rnd*380+10, Rnd*380+10, Rnd*380+10, Rnd*&hFFFFFF)
    Next
    Sleep 2000
Loop Until InKey <> "" ' loop until a key is pressed
 
End