Sometimes, computers do things that are completely counter-intuitive to us, and we find ourselves having to step back as programmers and simply say, "WOW!!" Here's a perfect example of that:
PreCalcCircles
k = 15
TestLoopLimit = 10000
FOR i
= 1 TO TestLoopLimit
CircleFillFast x, y, 300, k
FOR i
= 1 TO TestLoopLimit
CircleFill x, y, 300, k
PRINT USING "##.#### seconds with CircleFillFast"; t2
- t1
PRINT USING "##.#### seconds with CircleFill"; t3
- t2
FOR i
= 0 TO Radius
'each circle, for all radius sizes from 1 to limit FOR j
= 0 TO i
'get the points for each line of those circles CP
(i
, j
) = SQR(i
* i
- j
* j
)
SUB CircleFillFast
(x
, y
, Radius
, k
) FOR j
= 0 TO Radius
'get the points for each line of those circles LINE (x
- CP
(Radius
, j
), y
+ j
)-(x
+ CP
(Radius
, j
), y
+ j
), k
, BF
LINE (x
- CP
(Radius
, j
), y
- j
)-(x
+ CP
(Radius
, j
), y
- j
), k
, BF
RadiusError = -Radius
X = Radius
Y = 0
' Draw the middle span here so we don't draw it twice in the main loop,
' which would be a problem with blending turned on.
LINE (CX
- X
, CY
)-(CX
+ X
, CY
), C
, BF
RadiusError = RadiusError + Y * 2 + 1
LINE (CX
- Y
, CY
- X
)-(CX
+ Y
, CY
- X
), C
, BF
LINE (CX
- Y
, CY
+ X
)-(CX
+ Y
, CY
+ X
), C
, BF
X = X - 1
RadiusError = RadiusError - X * 2
Y = Y + 1
LINE (CX
- X
, CY
- Y
)-(CX
+ X
, CY
- Y
), C
, BF
LINE (CX
- X
, CY
+ Y
)-(CX
+ X
, CY
+ Y
), C
, BF
Here we look at two different circle fill routines -- one, which I'd assume to be faster, which precalculates the offset needed to find the endpoints for each line which composes a circle, and another, which is the same old CircleFill program which I've shared countless times over the years with people on various QB64 forums.
When all is said and done though, CircleFill is STILL even faster than CircleFillFast, which pregenerates those end-points for us!
I've got to admit, I find these results rather shocking! (Thus the name I chose when naming the CircleFill-NotSoFastAfterall routine.) Apparently, in this case, the integer math used in CircleFill is faster than the time it takes for QB64 to look up those internal values from a preset array.
Who woulda thunk it?!!
Anywho, I thought I'd share, just so others could look over the two routines and compare. Maybe there's a way to improve the CircleFillFast so that it'd be faster than CircleFill, but if it is, I'm not seeing it at the moment.
It looks like CircleFill is still the fastest routine to use to rapidly fill a circle for us. :D