Basicprogramming(.org) > General questions and discussions

Method or Madness ;-))

**B+**:

Method or Madness, method to madness? ;D

I say it is crazy addictive ear worm started at Naalaa because no built-in circle drawing at all, which is maddening for graphics crazed hobby programmers.

Here is my latest filled circle algo in SmallBASIC time is 40% of my last best filled circle algo. Results are more dramatic for larger radii.

--- Code: ---'new circle algo test.bas for SmallBASIC 0.12.9 (B+=MGA) 2017-05-25 post

' MIT license info below for fCirc2 algo

const sqr12 = sqr(.5)

radius = 350

ox = xmax/2

oy = ymax/2

t0 = ticks

for i = 1 to 100

color (i mod 15 + 1)

fCirc ox, oy, radius

next

t = ticks - t0

? t;" ms to draw 100 of these filled circles (r =350) my old fastest algo."

delay 4000

t0 = ticks

for i = 1 to 100

color (i mod 15 + 1)

fCirc2 ox, oy, radius

next

t = ticks - t0

? t;" ms for same circle test with new fastest algo."

pause

sub fCirc(xx, yy, r)

r2 = r * r

for x = 0 to r

y = sqr(r2-x*x)

line xx-x, yy+y, xx-x, yy-y

line xx+x, yy+y, xx+x, yy-y

next

end sub

sub fCirc2(xx, yy, r)

'const sqr12 = sqr(.5) 'in main const section

r2 = r * r

sqr12r = sqr12*r

rect xx-sqr12r, yy-sqr12r, xx + sqr12r, yy+sqr12r filled

for x = 0 to sqr12r

y = sqr(r2-x*x)

line xx-x, yy+sqr12r, xx-x, yy+y

line xx-x, yy-sqr12r, xx-x, yy-y

line xx+x, yy+sqr12r, xx+x, yy+y

line xx+x, yy-sqr12r, xx+x, yy-y

next

for x = sqr12*r to r

y = sqr(r2-x*x)

line xx-x, yy+y, xx-x, yy-y

line xx+x, yy+y, xx+x, yy-y

next

end sub

##################################################################################

# The MIT License (MIT)

# Copyright (c) 2016-2017 B+=MGA

#

# Permission is hereby granted, free of charge, to any person obtaining a copy

# of this software and associated documentation files (the "Software"), to deal

# in the Software without restriction, including without limitation the rights

# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

# copies of the Software, and to permit persons to whom the Software is

# furnished to do so, subject to the following conditions:

#

# The above copyright notice and this permission notice shall be included in all

# copies or substantial portions of the Software.

#

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

# SOFTWARE.

##################################################################################

--- End code ---

(see attached)

Hi Marcus,

I would have posted in Naalaa a translation of SmallBASIC code but the madness is how Naalaa does math eg floats and integers, crazy confusing for a language that claims not to be advanced.

**jj2007**:

In my experience, the time to draw circles depends exclusively on what the graphics card can deliver. Calculating sinuses costs cycles, of course, but the filling and drawing costs much more.

Do you have an explanation why you see such big differences? Do you have timings?

Attached an example that fills a window with a hundred circles. Here is the source:

include \masm32\MasmBasic\Res\MbGui.asm ; MasmBasic

Dim Pens() As DWORD

Dim Brushes() As DWORD

For_ ct=0 To 99

MakePen Pens(ct), Rand(-1), width 9 ; use random colours

MakeBrush Brushes(ct), Rand(-1) ; also for the fill

Next

MakePath 123, Circle(200) ; radius as n/1000 of window rect

SetGlobals posX, posY, pen, brush

Event Paint

NanoTimer()

For_ ecx=0 To 99 ; ID pen/brush x y scaleX scaleY

GuiDraw 123, <Pens(ecx)/Brushes(ecx)>, <Rand(500)>, <Rand(550)>, <Rand(800)>, <Rand(800)>

Next

GuiText 100.0-220, 100.0-22, Str$("%i ms (resize window to refresh)", NanoTimer(ms)) ; ca 20ms on a Core i5

GuiEnd

**B+**:

Results are different on different systems of course, that is why we should be comparing one algo versus another.

In SmallBASIC there IS a significant speed improvement for large radii circles (attached my results with SB).

Johnno has just reported that on SdlBasic the new algo takes 3 times longer! I must check that, I do seem to remember getting worse results as well, maybe I still have my test code.

I do want to make clear we are NOT filling circles with PAINT because that coloring method fails when overlapping filled circles over all sorts of previous drawn items.

Of course SB's built-in circle fill blows away these numbers but some BASIC's do not have built-in Circle fills Naalaa and QB64 for example.

**B+**:

I have now translated to SdlBasic the SmallBASIC test code and I get same fine results!

--- Code: ---' New circle algo test.sdlbas (B+=MGA) 2017-05-26

' Johnno has reported 3 times slower not faster than SmallBASIC results

' Here is my code check from

' new circle algo test.bas for SmallBASIC 0.12.9 (B+=MGA) 2017-05-25 post

' MIT license info below for fCirc2 algo

option qbasic

const xmax = 1200

const ymax = 700

setdisplay(xmax, ymax, 32, 1)

setcaption("New circle algo test For SdlBasic")

const sqr12 = sqr(.5)

radius = 350

ox = xmax/2

oy = ymax/2

t0 = ticks

for i = 1 to 100

ink(rgb(rnd(255), rnd(255), rnd(255)))

fCirc( ox, oy, radius)

next

t = ticks - t0

text(10, 20, 16, Str( t) + " ms to draw 100 of these filled circles (r =350) my old fastest algo.")

wait( 4000)

t0 = ticks

for i = 1 to 100

ink(rgb(rnd(255), rnd(255), rnd(255)))

fCirc2(ox, oy, radius)

next

t = ticks - t0

text(10, 40, 16, str(t) + " ms for same circle test with new fastest algo.")

waitkey(32)

end

sub fCirc(xx, yy, r)

r2 = r * r

for x = 0 to r

y = sqr(r2-x*x)

line( xx-x, yy+y, xx-x, yy-y)

line( xx+x, yy+y, xx+x, yy-y)

next

end sub

sub fCirc2(xx, yy, r)

'const sqr12 = sqr(.5) 'in main const section

r2 = r * r

sqr12r = sqr12*r

bar( xx-sqr12r, yy-sqr12r, xx + sqr12r, yy+sqr12r)

for x = 0 to sqr12r

y = sqr(r2-x*x)

line( xx-x, yy+sqr12r, xx-x, yy+y)

line( xx-x, yy-sqr12r, xx-x, yy-y)

line( xx+x, yy+sqr12r, xx+x, yy+y)

line( xx+x, yy-sqr12r, xx+x, yy-y)

next

for x = sqr12*r to r

y = sqr(r2-x*x)

line(xx-x, yy+y, xx-x, yy-y)

line( xx+x, yy+y, xx+x, yy-y)

next

end sub

'##################################################################################

'

'# The MIT License (MIT)

'# Copyright (c) 2016-2017 B+=MGA

'#

'# Permission is hereby granted, free of charge, to any person obtaining a copy

'# of this software and associated documentation files (the "Software"), to deal

'# in the Software without restriction, including without limitation the rights

'# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

'# copies of the Software, and to permit persons to whom the Software is

'# furnished to do so, subject to the following conditions:

'#

'# The above copyright notice and this permission notice shall be included in all

'# copies or substantial portions of the Software.

'#

'# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

'# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

'# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

'# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

'# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

'# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

'# SOFTWARE.

'

'##################################################################################

--- End code ---

I have to say, in most cases my graphics translations to SdlBasic run faster than in SmallBASIC which is why I am a fan of SdlBasic (plus you can do allot more with sound and graphics!) So here we have an unusual case where SmallBASIC is running faster than SdlBasic. Believe me this is rare.

**B+**:

Andy Amaya has brought to my attention Bresenham which saves time by avoiding SQR so here are 3 circle algo's and results in SmallBASIC.

--- Code: ---'3 fill circle tests.bas for SmallBASIC 0.12.9 (B+=MGA) 2017-05-27 post

' MIT license info below for fCirc2 algo

' Andy Amaya has brought to my attention the Bresenham Circle Fill ago

' from Andy Amaya at The QB64 Edition post is in Blitz

';Source:

'; http://homepage.smc.edu/kennedy_john/papers.htm

'; http://homepage.smc.edu/kennedy_john/bcircle.pdf

'

'; A Fast Bresenham Type Algorithm For Drawing Circles

'; by

'; John Kennedy

'

'; Blitzplus/Blitz 3D port by Andy_A

' Thanks Andy! best one yet! see fCircB(x, y, r)

const sqr12 = sqr(.5)

radius = 350

ox = xmax/2

oy = ymax/2

t0 = ticks

for i = 1 to 100

color (i mod 15 + 1)

fCirc ox, oy, radius

next

t = ticks - t0

? t;" ms to draw 100 of these filled circles (r =350) my old fastest algo."

delay 4000

t0 = ticks

for i = 1 to 100

color (i mod 15 + 1)

fCirc2 ox, oy, radius

next

t = ticks - t0

? t;" ms for same circle test with new fastest algo."

delay 4000

t0 = ticks

for i = 1 to 100

color (i mod 15 + 1)

fCircB ox, oy, radius

next

t = ticks - t0

? t;" ms for same circle test with Bresenham algo."

pause

sub fCirc(xx, yy, r)

local r2, x, y

r2 = r * r

for x = 0 to r

y = sqr(r2-x*x)

line xx-x, yy+y, xx-x, yy-y

line xx+x, yy+y, xx+x, yy-y

next

end sub

sub fCirc2(xx, yy, r)

local r2, sqr12r, x, y

'const sqr12 = sqr(.5) 'in main const section

r2 = r * r

sqr12r = sqr12*r

rect xx-sqr12r, yy-sqr12r, xx + sqr12r, yy+sqr12r filled

for x = 0 to sqr12r

y = sqr(r2-x*x)

line xx-x, yy+sqr12r, xx-x, yy+y

line xx-x, yy-sqr12r, xx-x, yy-y

line xx+x, yy+sqr12r, xx+x, yy+y

line xx+x, yy-sqr12r, xx+x, yy-y

next

for x = sqr12*r to r

y = sqr(r2-x*x)

line xx-x, yy+y, xx-x, yy-y

line xx+x, yy+y, xx+x, yy-y

next

end sub

'Bresenham see Source notes above

sub fCircB(CX, CY, R) 'thanks Andy Amaya for heads up

Local X, Y

Local XChange, YChange

Local RadiusError

X = R

Y = 0

XChange = 1 - (R Lshift 1)

YChange = 1

RadiusError = 0

While X >= Y

Line CX-X, CY+Y, CX+X, CY+Y ';used calc'd values to draw

Line CX-X, CY-Y, CX+X, CY-Y ';scan lines from points

Line CX-Y, CY+X, CX+Y, CY+X ';in opposite octants

Line CX-Y, CY-X, CX+Y, CY-X

Y = Y + 1

RadiusError = RadiusError + YChange

YChange = YChange + 2

if (RadiusError Lshift 1) + XChange > 0 then

X = X - 1

RadiusError = RadiusError + XChange

XChange = XChange + 2

End If

Wend

End

##################################################################################

# The MIT License (MIT)

# Copyright (c) 2016-2017 B+=MGA

#

# Permission is hereby granted, free of charge, to any person obtaining a copy

# of this software and associated documentation files (the "Software"), to deal

# in the Software without restriction, including without limitation the rights

# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

# copies of the Software, and to permit persons to whom the Software is

# furnished to do so, subject to the following conditions:

#

# The above copyright notice and this permission notice shall be included in all

# copies or substantial portions of the Software.

#

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

# SOFTWARE.

##################################################################################

--- End code ---

Thanks Andy Amaya best time yet!

Navigation

[0] Message Index

[#] Next page

Go to full version