Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Another small filled circe sub (not as fast as fcirc)
#37
A few changes to make comparing the speeds and overall results a little easier to understand the overall differences:

Code: (Select All)
Screen _NewImage(1200, 700, 32)
_Define A-Z As LONG
Dim Shared CircleXPos(500, 500)
PreCalcX



Print
Print " fcirc fc fc3 fc4 precalc"
Print
For i = 1 To 10

'time the fcirc sub
t## = Timer(0.001)
For c = 1 To 15000
fcirc 400, 400, 200, _RGB(128, 128, 128)
Next
t1## = Timer(0.001)

'time the fc sub
For c = 1 To 15000
fc 400, 400, 200, _RGB(255, 128, 128)
Next
t2## = Timer(0.001)

'time the fc3 sub
For c = 1 To 15000
FC3 400, 400, 200, _RGB(128, 255, 128)
Next
t3## = Timer(0.001)

'time the fc4 sub
For c = 1 To 15000
fc4 400, 400, 200, _RGB(128, 128, 255)
Next
t4## = Timer(0.001)

'time the Steve sub
For c = 1 To 15000
StevePreCalcCircle 400, 400, 200, _RGB(255, 0, 0)
Next
t5## = Timer(0.001)

Print t1## - t##, t2## - t1##, t3## - t2##, t4## - t3##, t5## - t4##
f1## = f1## + t1## - t##
f2## = f2## + t2## - t1##
f3## = f3## + t3## - t2##
f4## = f4## + t4## - t3##
f5## = f5## + t5## - t4##
Next
Print
Print f1##, f2##, f3##, f4##, f5##


Sub fc (cx, cy, r, clr&)
For y = -r To r
x = Int(Sqr(r * r - y * y))
Line (cx - x, cy + y)-(cx + x, cy + y), clr&, BF
Next
End Sub

Sub fc4 (cx, cy, r, clr&)
r2 = r * r
For y = 0 To r
y2 = y * y
'If y2 <= r2 Then
x = Int(Sqr(r2 - y2))
Line (cx - x, cy - y)-(cx + x, cy - y), clr&, BF
Line (cx - x, cy + y)-(cx + x, cy + y), clr&, BF
'End If
Next
End Sub

Sub FC3 (cx, cy, r, clr&)
Line (cx - r, cy)-(cx + r, cy), clr&, BF
y = 1
r2 = r * r ' Dav mod
While y <= r
y2 = y * y
If y2 < r2 Then
x = Int(Sqr(r2 - y2))
Line (cx - x, cy + y)-(cx + x, cy + y), clr&, BF
Line (cx - x, cy - y)-(cx + x, cy - y), clr&, BF
End If
y = y + 1
Wend
End Sub


Sub fcirc (CX As Integer, CY As Integer, R As Integer, C As _Unsigned Long)
Dim Radius As Integer, RadiusError As Integer
Dim X As Integer, Y As Integer
Radius = Abs(R): RadiusError = -Radius: X = Radius: Y = 0
If Radius = 0 Then PSet (CX, CY), C: Exit Sub
Line (CX - X, CY)-(CX + X, CY), C, BF
While X > Y
RadiusError = RadiusError + Y * 2 + 1
If RadiusError >= 0 Then
If X <> Y + 1 Then
Line (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
Line (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
End If
X = X - 1
RadiusError = RadiusError - X * 2
End If
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
Wend
End Sub


Sub StevePreCalcCircle (cx, cy, r, clr&)
$Checking:Off
For y = 0 To r
x = CircleXPos(r, y)
Line (cx - x, cy - y)-(cx + x, cy - y), clr&, BF
Line (cx - x, cy + y)-(cx + x, cy + y), clr&, BF
Next
$Checking:On
End Sub

Sub PreCalcX
For r = 0 To 500 'circles with a radius from 0 to 500
r2 = r * r
For y = 0 To r
y2 = y * y
x = Int(Sqr(r2 - y2))
CircleXPos(r, y) = x
Next
Next
End Sub


   

From this, on my machine, the precalculated coordinate look-up is the quickest, with fcirc right behind and everything else being a little slower. This is with the latest QB64PE version and with the optimization flag set in compiler options. I don't know if others will see the same style results or not, depending on however it compiles and optimizes on their systems.
Reply


Messages In This Thread
RE: Another small filled circe sub (not as fast as fcirc) - by SMcNeill - 08-30-2024, 05:59 AM



Users browsing this thread: 55 Guest(s)