Besenham's LineDraw & speed !

BlitzMax Forums/BlitzMax Beginners Area/Besenham's LineDraw & speed !

Hi ! Need some help here !

From the code archive, posted by angel daniel :

 'Besenham's LineDraw routine (integer math only) by AngelDaniel (Posted 4 months ago)  
  
 
'This program is a conversion from a C program taken from the Wikipedia, based on the line-drawing algorithm by Mr Bresenham. It uses purely Integer math only - absolutely no floats. It plots one pixel at a time. Clipping is handled by the standard clipping of the backbuffer, by the Graphics hardware For each pixel plotted (which is a Vertex in OpenGL). This isn't an OpenGL-dependent routine, you can easily use some other Plot routine. Each pixel can be a different color, or you could skip pixels, do dotted/dashed lines, marching ants, etc  
'Bresenham linedraw in BlitzMax, adapted from C code

Strict
Graphics 640,480,0
Repeat
	Cls
	Line(320,240,MouseX(),MouseY(),2)
	Flip
Until KeyHit(KEY_ESCAPE)
End

Function Line(X1:Int,Y1:Int,X2:Int,Y2:Int, speed:Int)

	
	'Draws a line of individual pixels from X1,Y1 to X2,Y2 at any angle
 	Local Steep:Int=Abs(Y2-Y1) > Abs(X2-X1)			'Boolean
	If Steep
		Local Temp:Int=X1; X1=Y1; Y1=Temp		'Swap X1,Y1
		Temp=X2; X2=Y2; Y2=Temp		'Swap X2,Y2
	EndIf
	Local DeltaX:Int=Abs(X2-X1)		'X Difference
	Local DeltaY:Int=Abs(Y2-Y1)		'Y Difference
	Local Error:Int=0		'Overflow counter
	Local DeltaError:Int=DeltaY		'Counter adder
	Local X:Int=X1		'Start at X1,Y1
	Local Y:Int=Y1		
	Local XStep:Int
	Local YStep:Int
	If X1<X2 Then XStep=1 Else XStep=-1	'Direction
	If Y1<Y2 Then YStep=1 Else YStep=-1 'Direction
	If Steep Then Plot(Y,X) Else Plot(X,Y)		'Draw
	While X<>X2
		X:+XStep		'Move in X
		Error:+DeltaError		'Add to counter
		If (Error Shl 1)>DeltaX		'Would it overflow?
			Y:+YStep		'Move in Y
			Error=Error-DeltaX		'Overflow/wrap the counter
		EndIf
		If Steep Then Plot(Y,X) Else Plot(X,Y)		'Draw
	Wend
End Function 


I use this method to move my ennemy along a line (point by point). It works fine. Now I'm trying to adapt this to move at speed 3 or more ! It should be easy to do but i've some difficulties to do this little thing in my game code !

i've modified these lines

	If X1<X2 Then XStep=1*Speed Else XStep=-1*Speed	'Direction
	If Y1<Y2 Then YStep=1*Speed Else YStep=-1*Speed 'Direction


but not work !
Could you help me ! Thanks !

It wont work if speed is greater than the difference between x and y ..

or in other words, bresenhams works by constantly `counting` an index by adding the step to it, and when the index gets over a certain value it `wraps around` like a modulo. Adding 1*Speed is probably not wrapping around properly.

You probably would need to run the loop 3 times and THEN draw on the third time?

ie If Counter Mod 3=0 Then Draw

ok thanks !

I personally prefer using sin and cosin:

dirx# = cos(angle)
diry# = sin(angle)
speed# = 2.0
while angle not changed
.
.
x# = x#+dirx#*speed#
y# = y#+diry#*speed#
plot x#,y#
.
.

wend
and sence most predefined BM commands are in floats then
I don't have to worry about casting problems.

I hope is of use to somebody. no harm intended.