Just got a second to expand on this.
In rough, the algorithm is as follows:
Code:
DrawLine(startx, starty, endx, endy):
dx = ABS(endx-startx)
dy = ABS(endy-starty)
dirx = SGN(endx-startx)
diry = SGN(endy-starty)
IF (dx>dy) THEN
; Plot a 'shallow' line, i.e. one which is more horizontal than vertical
accum = dx DIV 2
FOR count=0 TO dx
PlotPixel(startx, starty)
startx += dirx
accum -= dy
IF (accum<0)
accum += dx
starty += diry
ENDIF
NEXT
ELSE
; Plot a 'steep' line, i.e. one which is more vertical than horizontal
accum = dy DIV 2
FOR count=0 TO dy
PlotPixel(startx, starty)
starty += diry
accum -= dx
IF (accum<0)
accum += dy
startx += dirx
ENDIF
NEXT
ENDIF
In practice there are some optimisations to make, e.g. you can just calculate the screen address of the first pixel, and modify it as you increase startx/starty, instead of redoing the calculation each time in PlotPixel.
You may want to swap the start and end points at the start so you always deal with a line going from top to bottom (for example), which removes the need to store diry.
If you're EORing pixels, you might find that it's better not to plot the endpoint in a line, so that subsequent lines which start at the endpoint of the last one don't replot the pixel, causing it to disappear. You can do this simply by starting the FOR counter from 1 instead of 0 in both cases ('shallow' and 'steep' lines).
I had a good optimised 6502 Bresenham line drawing routine somewhere, but have no idea where to look at the moment. If I can dig it out, I'll post it here.