Hey guys,
I just now needed a line algorithm ( not actually drawing lines, so I couldn't settle for the Line() command ), and so I googled, and got two major versions, both of which are variations on Breshnam's Algorithm...
Now, it seems as though the first should be faster ( less to do in the 'For' part ), but it turns out that both run at the same speed ( apparently ).
Just so I can be certain that my computer isn't being difficult, can you test it for me?
Sync On : Sync Rate 0
Cls RGB(255,255,255)
Get Image 1,0,0,Screen Width(), Screen Height()
Make Memblock From Image 1,1
Global st1, t1, st2, t2 As Integer
st1 = Timer()
For i = 0 To 5000
_b_line_1(1,10,10,210,110, RGB(0,0,0) || (255 << (8*3)))
_b_line_1(1,10,10,110,210, RGB(0,0,0) || (255 << (8*3)))
_b_line_1(1,110,10,10,210, RGB(0,0,0) || (255 << (8*3)))
_b_line_1(1,210,10,10,110, RGB(0,0,0) || (255 << (8*3)))
_b_line_1(1,210,110,10,10, RGB(0,0,0) || (255 << (8*3)))
_b_line_1(1,110,210,10,10, RGB(0,0,0) || (255 << (8*3)))
_b_line_1(1,10,210,110,10, RGB(0,0,0) || (255 << (8*3)))
_b_line_1(1,10,110,210,10, RGB(0,0,0) || (255 << (8*3)))
Next i
t1 = Timer() - st1
st2 = Timer()
For i = 0 To 5000
_b_line_2(1,10,10,210,110, RGB(0,0,0) || (255 << (8*3)))
_b_line_2(1,10,10,110,210, RGB(0,0,0) || (255 << (8*3)))
_b_line_2(1,110,10,10,210, RGB(0,0,0) || (255 << (8*3)))
_b_line_2(1,210,10,10,110, RGB(0,0,0) || (255 << (8*3)))
_b_line_2(1,210,110,10,10, RGB(0,0,0) || (255 << (8*3)))
_b_line_2(1,110,210,10,10, RGB(0,0,0) || (255 << (8*3)))
_b_line_2(1,10,210,110,10, RGB(0,0,0) || (255 << (8*3)))
_b_line_2(1,10,110,210,10, RGB(0,0,0) || (255 << (8*3)))
Next i
t2 = Timer() - st2
Delete Image 1
Make Image From Memblock 1,1
Ink RGB(50,200,50),0
Paste Image 1,0,0
Text 0,0,"For 5000 itterations each..."
Text 0,30,"Function 1: " + Str$(t1) + "ms"
Text 0,45,"Function 2: " + Str$(t2) + "ms"
Do
Sync
Loop
Function _dot(mem As Integer,x As Integer,y As Integer,colour As DWord)
`Basic 'Memblock' Faster-than Dot() command
pos = 12 + (x * 4) + (y * MemBlock DWord(mem,0) * 4)
If pos >= 12 And pos < Get MemBlock Size(mem) Then Write MemBlock DWord mem,pos,colour
EndFunction
Function _b_line_1(mem As Integer, x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer, colour As DWord)
Local steep As Boolean
Local x, y, tmpint, dx, dy, error, derr, ystep As Integer
If Abs(y2 - y1) > Abs(x2 - x1) Then steep = 1
If steep = 1
tmpint = x1 : x1 = y1 : y1 = tmpint
tmpint = x2 : x2 = y2 : y2 = tmpint
EndIf
If x1 > x2
tmpint = x1 : x1 = x2 : x2 = tmpint
tmpint = y1 : y1 = y2 : y2 = tmpint
EndIf
dx = x2 - x1
dy = Abs(y2 - y1)
error = 1
derr = dy
y = y1
If y1 < y2 Then ystep = 1 Else ystep = -1
For x = x1 To x2
If steep = 1 Then _dot(1,y,x,colour) Else _dot(1,x,y,colour)
Inc error, derr
If error << 1 >= dx
Inc y, ystep
Dec error, dx
EndIf
Next x
EndFunction
Function _b_line_2(mem As Integer, x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer, colour As DWord)
Local x, y, dx, dy, xinc1, xinc2, yinc1, yinc2, den, num, numadd, numpixels As Integer
dx = Abs(x2 - x1)
dy = Abs(y2 - y1)
x = x1
y = y1
If x2 >= x1 `The x-values are increasing
xinc1 = 1
xinc2 = 1
Else `The x-values are decreasing
xinc1 = -1
xinc2 = -1
EndIf
If y2 >= y1 `The y-values are increasing
yinc1 = 1
yinc2 = 1
Else `The y-values are decreasing
yinc1 = -1
yinc2 = -1
EndIf
If dx >= dy `There is at least one x-value for every y-value
xinc1 = 0 `Don't change the x when numerator >= denominator
yinc2 = 0 `Don't change the y for every iteration
den = dx
num = dx / 2
numadd = dy
numpixels = dx `There are more x-values than y-values
Else
xinc2 = 0 `Don't change the x for every iteration
yinc1 = 0 `Don't change the y when numerator >= denominator
den = dy
num = dy / 2.0
numadd = dx
numpixels = dy `There are more y-values than x-values
EndIf
For i = 0 To numpixels
`PutPixel(x,y)
_dot(1,x,y,colour)
Inc num, numadd `Increase the numerator by the top of the fraction
If num >= den `Check if numerator >= denominator
Dec num, den `Calculate the new numerator value
Inc x, xinc1
Inc y, yinc1
EndIf
Inc x, xinc2
Inc y, yinc2
Next i
EndFunction
As you can see, there's two versions in there, each one is run 5000 times for each possible orientation ( so that all branching coditions are tested ) giving a grand total of 80000 lines drawn
I get it reading about 1460 ( or something about that ) for both, each time I run it... Strange, no?
Anyhoo, If you could give 'er a whirl, I'd muchly appreciate it, thanks
Jess.
[EDIT] Just realized I left a 'Cls' in the second one... Am re-testing now [/EDIT]
[EDIT2]
Ok, after re-testing, I still get exactly the same... I'm starting to think that this function's so blisteringly fast that I'll have to use the High-Res Windows timer within the function itself, return the time from it and add it up that way ( bypasses all the 'For' and 'Go find that function' calls )...
[/EDIT2]
[EDIT3]
Damn typo... I put in an extra number for the times I was getting...

- Fixed now ( should be 1460, and I had something like 14250 )
[/EDIT]