Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBC/DBP] Fill circle by looping through less than 1/4 of the pixels

Author
Message
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 19th Jan 2009 21:06
Not sure where this idea came from, I think I may have heard or seen the idea elsewhere. But since a circle is symmetrical on both X and Y, a few simple checks can draw a filled-in circle.

DBC


DBP


The reason for the slightly two different versions is in how the BOX command works. DBC seems to be inclusive of the coordinates, where a box from 1,1 to 1,1 would fill 1 pixel. But in DBP, a box with the same coordinates would draw nothing and needs 1,1 to 2,2. At least stretching the circle out 1 extra pixel in pro then gave me equal results between classic and pro.

Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 21st Jan 2009 18:35
You could make it faster by using box to draw lines, and take advantage of the diagonal symmetry. Also using * instead of the ^2 might make it a little faster, though i suspect the computation is a lot quicker than the actual drawing.

AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 21st Jan 2009 19:09
Ya, I gained a 40% performance gain by changing the exponents to multiplication. Here's the changed code:



Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 21st Jan 2009 21:39
Not sure what you mean tank, I'm already using box to draw the lines

Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 23rd Jan 2009 01:45 Edited at: 23rd Jan 2009 01:49
Quote: "Not sure what you mean tank, I'm already using box to draw the lines
"

Sorry. I misread your code. I thought that because you were looping over x and y you must have been drawing individual pixels with the box command, but i see what you did now. Quite cunning. I think you could maybe gain by computing the ends of each line using sqrt.. (DBP version)



Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 23rd Jan 2009 02:15
ooo clever, but I wonder if the sqrt() affects performance. Fewer loops, but we all know how slow sqrt can be.

Your signature has been erased by a mod because it's larger than 600x120
Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 23rd Jan 2009 02:28
it's a good question. should do a speed test. Avoiding sqrt, i made the following optimisation. NB i am still using ^2. lose that and it'll get quicker still..



Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 23rd Jan 2009 02:39 Edited at: 23rd Jan 2009 02:50
OK check it out. 4 versions of the function in order of speed, with the box commands commented out. Your original first, then an altered version with different looping, the same again with ^2 not used, then the sqrt version. The sqrt one is significantly quicker.



here's with the drawing commands back in:



BTW i have no idea why the 3rd command appears to be slower than the 2nd one!! Also, you can get a tiny bit more speed by calculating r*r at the start of the function.

Mr Kohlenstoff
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 24th Jan 2009 18:31
Interesting, I did something similar some months ago, don't know where the code is however.

@MrTank: The third one seems to be the fastest here, around 50% faster than 2 and 4. The first function is around 200 times slower.

IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 24th Jan 2009 19:21
Has anyone tried the version I put into codebase a 'short' while back?

http://www.thegamecreators.com/?m=codebase_view_code&i=c6a376253a019691f5fcc97de1c5247a

There are still a few possible cycles that can be squeezed out of it if anyone wants to bother.

Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 25th Jan 2009 04:54 Edited at: 25th Jan 2009 04:56
Quote: "@MrTank: The third one seems to be the fastest here, around 50% faster than 2 and 4. The first function is around 200 times slower."

Interesting. For me 4 is way faster than 2 or 3, but maybe square roots are slower relative to normal operations for you than they are for me. Wierd.

Quote: "Has anyone tried the version I put into codebase a 'short' while back?"

Dude that's sweet. I hadn't thought to but the box in the middle.

Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 28th Feb 2009 21:03
This code will draw random filled circles with an average radius of 37 pixels, with an average speed of 0.24 milliseconds per circle. I don't know how that compares with the others:



[b]Yuor signutare was aresed by a deslyxic mud...
BOX2D V2 HAS HELP FILES! AND A WIKI!
calcyman
16
Years of Service
User Offline
Joined: 31st Aug 2007
Location: The Uncertainty Principle
Posted: 2nd Mar 2009 21:49
Have you considered using lock pixels/unlock pixels to make rendering faster?

The optimist's right, The pessimist's right.
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 2nd Mar 2009 22:10
Yes, but it doesn't (or didn't use to) have an effect on drawing speed with the BOX command because BOX uses DirectX to render it.

Login to post a reply

Server time is: 2024-05-05 05:25:26
Your offset time is: 2024-05-05 05:25:26