Code archives/Graphics/Draw Line

This code has been declared by its author to be Public Domain code.

Download source code

Draw Line by bytecode772007
it was usualy intended for my uses, but i post it here.
remember that the line you see is drawn 10.000 times to measure the correct draw time!
Graphics 640, 480, 32, 2
SetBuffer BackBuffer()

While Not KeyHit(1)
	LockBuffer()
	ms = MilliSecs()
	mx = MouseX()
	my = MouseY()
	For i = 1 To 10000
		Line2 320, 240, mx, my
	Next
	ms = MilliSecs() - ms
	UnlockBuffer()
	Text 10, 10, "Millisecs per line: " + Float(ms * .0001)
	Text 10, 30, "The line you see is drawn 10.000 times to measure the correct time!!"
	Flip 0
	Cls
Wend
End

Function Line2(p1x, p1y, p2x, p2y)
If Abs(p1y - p2y) <= Abs(p1x - p2x) Then
	If p1x > p2x Then
		ptx = p1x
		pty = p1y
		p1x = p2x
		p1y = p2y
		p2x = ptx
		p2y = pty
	EndIf
	For x = p1x To p2x
		y = p1y + (x - p1x) * (p2y - p1y) / (p2x - p1x)
		WritePixelFast x, y, $FFFFFF
	Next
Else
	If p1y > p2y Then
		ptx = p1x
		pty = p1y
		p1x = p2x
		p1y = p2y
		p2x = ptx
		p2y = pty
	EndIf
	x = p1x
	For y = p1y To p2y
		x = p1x + (y - p1y) * (p2x - p1x) / (p2y - p1y)
		WritePixelFast x, y, $FFFFFF
	Next
EndIf
End Function

Comments

CS_TBL2007
How does the performance compare with Bresenham?


bytecode772007
i dont know bresenham exactly, but it works as follows:

there are two cases:
1. the line has more width than height
the y coordinates are interpolated along the x coordinates
2. the line has more height than width
the x coordinates are interpolated along the y coordinates


CS_TBL2007
Bresenham works without division, and the only multiplication is a *2 ..


Code Archives Forum