Bresenham's line algorithm

BlitzMax Forums/BlitzMax Programming/Bresenham's line algorithm

This example uses a modified LockedLine routine from a Blitz3D example (skidracers I think ?)

' Bresenham Line example (Draws to pixmap)

Const sw=640 , sh=480

Graphics sw,sh,0,NOSYNC
pix:TPixmap=CreatePixmap(sw,sh,PF_RGB888)

For Local n=1 To 100
	BLine pix,Rand(sw),Rand(sh),Rand(sw),Rand(sh),Rand(16777216)
Next

DrawPixmap pix,0,0
Flip ; WaitKey
End

Function BLine(pm:TPixmap,x1#,y1#,x2#,y2#,rgb)
	Local steps , xI#
	x2:-x1 ; y2:-y1
	If Abs(x2)>Abs(y2) steps = Abs(x2) Else steps = Abs(y2)
	xI = x2 / steps ; y2:/steps
	For x2 = 1 To steps
 		WritePixel pm,x1,y1,rgb
		x1:+xI ; y1:+y2
	Next
End Function