Bresenham's line algorithm

taximania

Well-Known Member
Licensed User
Longtime User
I'm sending data to a graphic LCD via Bluetooth.
I've only got a PlotPixel(x,y) available.

Does anybody have an example of Line(x1,x2,y1,y2)

I've googled but can't find a 'basic' algorithm, plenty in 'C' and VB,
but it's doing my head in trying to convert it.

TIA
 

agraham

Expert
Licensed User
Longtime User
Here's the real thing, it won't leave gaps on steep lines.
B4X:
Sub BresenhamLine(x0, y0, x1, y1)
  steep = False
  If Abs(y1 - y0) > Abs(x1 - x0) Then
    steep = True
    'swap(x0, y0)
    x = x0 : x0 = y0 : y0 = x      
    'swap(x1, y1)
    x = x1 : x1 = y1 : y1 = x   
  End If
  If x0 > x1 Then
    'swap(x0, x1)
    x = x0 : x0 = x1 : x1 = x            
    'swap(y0, y1)
    x =y0 : y0 = y1 : y1 = x
  End If
  deltax = Int(x1 - x0)
  deltay = Int(Abs(y1 - y0))
  error = Int(deltax / 2)
  y = y0
  If y0 < y1 Then
    ystep = 1
  Else
    ystep = -1
  End If
  For x = x0 To x1
    If steep Then plot(y,x) Else plot(x,y)
    error = error - deltay
    If error < 0 Then
      y = y + ystep
      error = error + deltax
    End If
  Next
End Sub
 

taximania

Well-Known Member
Licensed User
Longtime User
Here's the real thing, it won't leave gaps on steep lines.

After a few test, your definately right.

The attached program works best on the desktop. It does work on the device but the SIP covers the controls.

Circle shows a circle :sign0060:

Rose draws a shape :signOops:

Sin draws a sin wave :sign0148:

The subs code maybe of help :cool:

If you enter the x1,y1,x2,y2 values eg:

100,0,90,200 and 'Bres' you get a good solid line.

then try

100,0,110,200 and 'mine'

That is a huge difference.

Cheer A
 

Attachments

  • plot.sbp
    2.9 KB · Views: 175
Top