January 23, 2019, 08:26:41 AM

### Author Topic: Mark's circles wtf  (Read 1164 times)

#### meems

• Sr. Member
• Posts: 311
##### Mark's circles wtf
« on: February 13, 2018, 10:22:10 AM »
so i always thought Mark Sibly was a great programmer. But even the best make a few errors.
I couldn't understand how he had such a slow circle oval draw function in blitz. So I went his blitzplus github page. And by golly, what a monster function. wtf was he thinking? Most of his functions are concise, but this one, he really went into an unnecessary myriad of protracted thinking and ideas just to draw a circle.

No wonder blitzers wrote their own circle and oval functions. I'm no high tier programmer, but I got my own blitzplus circle function running at 70~100 times faster than Mark's, using just 8 lines of simple code, w/o using writepixelfast.
If anyone gets a chance to talk to him, ask him about his circles wtf.

Code: [Select]
void BBGrDriver2D::oval( int x1,int y1,int w,int h,bool solid ){

BBRect dest(x1,y1,x1+w,y1+h);
if( !clipRect().clip( &dest ) ) return;

float xr=w*.5f,yr=h*.5f,ar=(float)w/(float)h;
float cx=x1+xr+.5f,cy=y1+yr-.5f,rsq=yr*yr,y;

if( solid ){
y=dest.top-cy;
for( int t=dest.top;t<dest.bottom;++y,++t ){
float x=(float)sqrt( rsq-y*y )*ar;
int xa=(int)floor( cx-x ),xb=(int)floor( cx+x );
rect( xa,t,xb-xa,1,true );
}
return;
}

int l,r,p_xa,p_xb,t,hh=(int)floor(cy);

p_xa=p_xb=(int)cx;
t=dest.top;y=t-cy;
if( dest.top>y1 ){ --t;--y; }
for( ;t<=hh;++y,++t ){
float x=(float)sqrt( rsq-y*y )*ar;
int xa=(int)floor( cx-x ),xb=(int)floor( cx+x );

l=xa;r=p_xa;if(r<=l)r=l+1;
rect( l,t,r-l,1,true );
l=p_xb;r=xb;if(l>=r)l=r-1;
rect( l,t,r-l,1,true );

p_xa=xa;p_xb=xb;
}

p_xa=p_xb=(int)cx;
t=dest.bottom-1;y=t-cy;
if( dest.bottom<y1+h ){ ++t;++y; }
for( ;t>hh;--y,--t ){
float x=(float)sqrt( rsq-y*y )*ar;
int xa=(int)floor( cx-x ),xb=(int)floor( cx+x );

l=xa;r=p_xa;if(r<=l)r=l+1;
rect( l,t,r-l,1,true );
l=p_xb;r=xb;if(l>=r)l=r-1;
rect( l,t,r-l,1,true );

p_xa=xa;p_xb=xb;
}
}

#### meems

• Sr. Member
• Posts: 311
##### Re: Mark's circles wtf
« Reply #1 on: February 13, 2018, 12:21:50 PM »
>just 8 lines of simple code
And on re-examine I see it can be 7 lines of simple code

#### ENAY

• Full Member
• Posts: 237
##### Re: Mark's circles wtf
« Reply #2 on: February 14, 2018, 10:07:08 AM »
I don't think Mark has ever been a man of Math. Besides who uses oval in their games anyway?

• Sr. Member
• Posts: 346
##### Re: Mark's circles wtf
« Reply #3 on: February 14, 2018, 10:25:27 AM »
Don't have the time to look real close, but it appears that most of that code is dealing with ovals at the edge of the screen.  You can't just simply shrink the bounding rectangle to the clipping plane or else your ovals will be squashed.
------------------------------------------------
8 rabbits equals 1 rabbyte.

#### meems

• Sr. Member
• Posts: 311
##### Re: Mark's circles wtf
« Reply #4 on: February 14, 2018, 10:46:36 AM »
@TomToad my circle code can draw over the edge of screen, writepixel implicitly handles attempts to draw out-of-bounds.

>Besides who uses oval in their games anyway?
I like the mario4 , looneytoons style of screen flip, which was circles-to-black in and out, so I had them in my games. Circles are also useful for games with bubbles, like underwater games.

#### Derron

• Hero Member
• Posts: 1560
##### Re: Mark's circles wtf
« Reply #5 on: February 14, 2018, 11:45:32 AM »
I assume "write pixel" is slower than "drawrect" (dozen of plots versus a single fill operation instruction).

Without having a deeper look at the code (or yours) using less draw calls is to prefer. Dunno if "draw line" was possible there too - but usage of "rect()" alone seems to say that there was some kind of optimization done. "rect" is a untextured quad, a line surely a connection of plots.

@ Faster
What did you measure? FPS or "function execution time" ? Depending on your GPU/driver-stack the work is done on "flip" rather than "draw".

bye
Ron

#### meems

• Sr. Member
• Posts: 311
##### Re: Mark's circles wtf
« Reply #6 on: February 14, 2018, 01:11:31 PM »
>seems to say that there was some kind of optimization done.
>some kind of optimization

Yes, a shit one.

I measured mine against Mark's in function execution time. For screenbuffer flip, Flip 0 was used.

#### Derron

• Hero Member
• Posts: 1560
##### Re: Mark's circles wtf
« Reply #7 on: February 14, 2018, 01:26:40 PM »
So where is yours?

bye
Ron

#### Steve Elliott

• Hero Member
• Posts: 1370
##### Re: Mark's circles wtf
« Reply #8 on: February 14, 2018, 02:32:09 PM »
Mark has written several computer languages - including one with a built-in 3D Engine.  Yet you sneer at one routine he wrote and say you can do better, but didn't post your version.

Hmm.  How many Computer Languages/3D Engines have you written then?  And of course they all contain perfect code.
Windows 10, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)

MacOS Mojave, 64-bit, 8Gb RAM, CPU Intel i5, 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB.

#### Derron

• Hero Member
• Posts: 1560
##### Re: Mark's circles wtf
« Reply #9 on: February 14, 2018, 02:43:51 PM »
@ Steve
Meems already started his thread with praise - and he wonders how it comes that such a mind as MS wrote this function. One could express this in a nicer way but the core is: if there are more efficient variants around there, why has MS written it the way it is now.

bye
Ron

#### Steve Elliott

• Hero Member
• Posts: 1370
##### Re: Mark's circles wtf
« Reply #10 on: February 14, 2018, 04:11:20 PM »
Fair point Derron, I was more reacting to the "Yes, a shit one." regards optimisation.  The tone is one of I can do much better.

But in a large project, anybody can let some unefficient code slip through.
Windows 10, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)

MacOS Mojave, 64-bit, 8Gb RAM, CPU Intel i5, 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB.

#### meems

• Sr. Member
• Posts: 311
##### Re: Mark's circles wtf
« Reply #11 on: February 14, 2018, 04:11:44 PM »
So where is yours?
Behold, a modest genius's finely crafted exhibit!

Code: [Select]
Function my_circle(x,y,r,col=222+(222 Shl 8)+(222 Shl 16))

x2#=r
y2#=0
n=r*6.29  ; if gaps in circle then increase this number, but should be ok

For i=0 To n
WritePixel x+x2,y+y2,col
x2=x2-y2/r
y2=y2+x2/r
Next

End Function

Actually, back in the blitzforum days, I saw others write their own circle and oval functions. We never got to the point of testing against each other to see whose was the fastest and most versatile. But some of us noobs were surprised we could beat Mark, and it made us feel like leet programmers for a moment.

#### Steve Elliott

• Hero Member
• Posts: 1370
##### Re: Mark's circles wtf
« Reply #12 on: February 14, 2018, 04:17:05 PM »
lol
Windows 10, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)

MacOS Mojave, 64-bit, 8Gb RAM, CPU Intel i5, 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB.

#### Derron

• Hero Member
• Posts: 1560
##### Re: Mark's circles wtf
« Reply #13 on: February 14, 2018, 05:19:23 PM »
Hmm, somehow I cannot use your function to do a solid circle - or even a non-solid oval?

bye
Ron

#### meems

• Sr. Member
• Posts: 311
##### Re: Mark's circles wtf
« Reply #14 on: February 14, 2018, 07:13:20 PM »
it doesn't do solids. try a loop like

Code: [Select]
function solid_circle(x,y,r)

for i=1 to r
my_circle(x,y,i)
next

end function
interesting to see if this is faster than Mark's