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.




#DEFINE WM_LBUTTONDOWN 513
#DEFINE WM_CLOSE 16
 
FBSLSETTEXT(ME, "Bresenham") ' Set form caption
FBSLSETFORMCOLOR(ME, RGB(0, 255, 255)) ' Cyan: set persistent background color
DRAWWIDTH(5) ' Adjust point size
FBSL.GETDC(ME) ' Use volatile FBSL.GETDC below to avoid extra assignments
 
RESIZE(ME, 0, 0, 200, 235)
CENTER(ME)
SHOW(ME)
 
BEGIN EVENTS
  SELECT CASE CBMSG
    CASE WM_LBUTTONDOWN: Rhombus() ' Draw
    CASE WM_CLOSE: FBSL.RELEASEDC(ME, FBSL.GETDC) ' Clean up
  END SELECT
END EVENTS
 
SUB Rhombus()
  Bresenham(50, 100, 100, 190)(100, 190, 150, 100)(150, 100, 100, 10)(100, 10, 50, 100)
 
  SUB Bresenham(x0, y0, x1, y1)
    DIM dx = ABS(x0 - x1), sx = SGN(x0 - x1)
    DIM dy = ABS(y0 - y1), sy = SGN(y0 - y1)
    DIM tmp, er = IIF(dx > dy, dx, -dy) / 2
 
    WHILE NOT (x0 = x1 ANDALSO y0 = y1)
      PSET(FBSL.GETDC, x0, y0, &HFF) ' Red: Windows stores colors in BGR order
      tmp = er
      IF tmp > -dx THEN: er = er - dy: x0 = x0 + sx: END IF
      IF tmp < +dy THEN: er = er + dx: y0 = y0 + sy: END IF
    WEND
  END SUB
END SUB