Draw that Circle
#1
I was playing with alternate means draw filled circles and had the most success with the following routine.

the basic algorithm is simple:  determine the center of the circle , plot the y axis out to the distance or R (negative to positive) and calculate X for each Y. Draw a line at X across the Y axis for each point of Y until there's a circle.

I was playing with a number of different methods to draw and do the calculations and the ones included here are good to share.

rcircle took  .8789062 to .9335938 seconds
dcircle took .6015625 to .6601562 seconds
dmcircle took .6015625 to .6601562 seconds

dcircle tended to get the best draw time on my computer, it had the same  range as dmcircle but it tended to be lower on average so certainly a win for the reference table but no clear advantage to using mem to access the look up table.

EDIT: typo corrected... better version of the rcircle sub is later in the thread

Code: (Select All)
'Draw_that_circle
'
'By James D. Jarvis
'
'an alternate subroutine to draw a filled circle.
'the basic sub is rcircle, rcircle find X while stepping through Y for -radius to the radius
'dcircle,  references the precalculated table in the array nsqr() to otherwise drae as rcircle
'dmcircle, references the precalculated table in the array nsqr() as dcircle but makes uses of _mem for access.

Screen _NewImage(800, 500, 256)
Randomize Timer
Dim Shared nsqr(-32000 To 32000) As Long 'table holds the square values
Dim Shared nsq As _MEM 'let's access the memblock of the table

For n = -32000 To 32000 'precaluclaitng the square values of -32000 to 32000
    nsqr(n) = Abs(n) ^ 2
Next
nsq = _Mem(nsqr())
Input "press enter to start"; any$

'draw without look up table
t1 = Timer
For n = 1 To 64000
    ' r = Int(1 + Rnd * 60) 'comment this out in each l0op to see the difference with a fixed radius
    r = 30 'chanign the size certainyl crerates a speed difference
    rcircle Int(Rnd * 800), Int(Rnd * 500), r, Int(Rnd * 256)
Next n
t2 = Timer
Print "that was"; t2 - t1; " seconds for 64,000 circles"

'draw with look up table
Input "Press enter to continue"; A$
t3 = Timer
For n = 1 To 64000
    'r = Int(1 + Rnd * 60)
    r = 30
    dcircle Int(Rnd * 800), Int(Rnd * 500), r, Int(Rnd * 256)
Next n
t4 = Timer
Print "that was"; t4 - t3; " seconds for 64,000 circles"
Input "press enter to continue"; ask$
'draw with look up table and using mem offset
t5 = Timer
For n = 1 To 64000
    'r = Int(1 + Rnd * 60)
    r = 30
    dmcircle Int(Rnd * 800), Int(Rnd * 500), r, Int(Rnd * 256)
Next n
t6 = Timer
Print "that was"; t6 - t5; " seconds for 64,000 circles"
Print
Print "Analysis"
Print "rcircle took  "; t2 - t1; " seconds"
Print "dcircle took  "; t4 - t3; " seconds"
Print "dmcircle took  "; t6 - t5; " seconds"

_MemFree nsq

Sub rcircle (cx, cy, r, klr)
'simple filled circle drawing with making use of paint command
    For y = -r To r
        x = Sqr(r ^ 2 - y ^ 2)
        Line (cx - x, cy + y)-(cx + x, cy + y), klr
        ' Print x
    Next y
End Sub

Sub dcircle (cx, cy, r, klr)
    'simple routine to draw a circle using a look up table to speed up calculations
    y = -r
    Do
        x = Sqr(nsqr(r) - nsqr(y))
        Line (cx - x, cy + y)-(cx + x, cy + y), klr
        y = y + 1
    Loop Until y > r
End Sub

Sub dmcircle (cx, cy, r, klr)
    'simple routine to draw a circle using a look up table to speed up calculations
    y = -r
    Do
        x = Sqr(_MemGet(nsq, nsq.OFFSET + (r + 32000) * 4, Long) - _MemGet(nsq, nsq.OFFSET + (y + 32000) * 4, Long))
        Line (cx - x, cy + y)-(cx + x, cy + y), klr
        y = y + 1
    Loop Until y > r
End Sub

Sub wcircle (cx, cy, r, klr)
    'was testing if there was a notable difference between using a do loop or a while loop, left it here if anyone else wanted to try it
    y = -r
    While y <= r
        x = Sqr(nsqr(r) - nsqr(y))
        Line (cx - x, cy + y)-(cx + x, cy + y), klr
        y = y + 1
    Wend
End Sub
Reply


Messages In This Thread
Draw that Circle - by James D Jarvis - 08-26-2022, 04:00 PM
RE: Draw that Circle - by bplus - 08-26-2022, 05:34 PM
RE: Draw that Circle - by bplus - 08-26-2022, 05:39 PM
RE: Draw that Circle - by SMcNeill - 08-26-2022, 05:49 PM
RE: Draw that Circle - by James D Jarvis - 08-26-2022, 05:56 PM
RE: Draw that Circle - by James D Jarvis - 08-26-2022, 05:49 PM
RE: Draw that Circle - by bplus - 08-26-2022, 05:54 PM
RE: Draw that Circle - by bplus - 08-26-2022, 05:59 PM
RE: Draw that Circle - by SMcNeill - 08-26-2022, 06:05 PM
RE: Draw that Circle - by SMcNeill - 08-26-2022, 05:59 PM
RE: Draw that Circle - by bplus - 08-26-2022, 06:18 PM
RE: Draw that Circle - by James D Jarvis - 08-26-2022, 06:33 PM
RE: Draw that Circle - by mnrvovrfc - 08-26-2022, 09:44 PM
RE: Draw that Circle - by justsomeguy - 08-28-2022, 01:26 AM
RE: Draw that Circle - by SMcNeill - 08-28-2022, 03:32 AM
RE: Draw that Circle - by justsomeguy - 08-28-2022, 04:13 AM
RE: Draw that Circle - by SMcNeill - 08-28-2022, 05:55 AM
RE: Draw that Circle - by justsomeguy - 08-28-2022, 06:29 AM



Users browsing this thread: 7 Guest(s)