  Bresenham's line algorithm

Discussion in 'Questions (Windows Mobile)' started by taximania, Nov 8, 2008.

1. 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

2. Ok, I've managed it, sort of Code:
`Sub Draw_Line(x1,y1,x2,y2)length = x2-x1m = (y2-y1)/(x2-x1)y = y1For i = 0 To lengthPlot(x1+i,Int(y))y = y + mNext iEnd Sub`

3. Here's the real thing, it won't leave gaps on steep lines.
Code:
`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  NextEnd Sub`

4. You never cease to amaze me Agraham.

I found this Atari Archive website Plotting - Computer Graphics.

Sin waves, circles, eliptical rose (?) etc, all work on my graphic Lcd.

My, 'Draw_line', only does 'right,up', or 'right,down'.
I'll try your code later :sign0188:

5. 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 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

File size:
2.9 KB
Views:
47