Skip to main content
gamedev.net
🔒 Locked

Circle drawing

Started by flashingchicken 4 replies 1k views
Original Post
flashingchicken
flashingchicken
Doing a SDL prog, all i need is to be able to draw a circle, and i couldnt get any of the primitive libraries to work (sdl_gfx, sge), so, i wrote myown, but, i wasnt sure if it was considered efficient at all, or if i should just try to get the outside libraries to work. DrawCircle(sx,sy,r,color) //(sx,sy) is center of circle { {theta,x,y}=0 do { x=r*sin(theta) y=r*cos(theta) DrawRect(sx-x,sy-y,x*2, y*2) theta=arctan( x / (y+1) ) }while (theta<pi and sy+y<screen_height) }
clb
clb
Not bad, I like the way you calculate the new angle with arctan.. but I think you do a lot of overdraw with DrawRect..

About what you're asking, whether you should get the SDL proprietary libs working or not.. well do what you want, basically. Using a library that's already been written obviously allows you to use the time for something else. If you want to implement your own circle drawing algorithm, you could look at
Bresenham's fast circle drawing algorithm. It's a little speed improvement over trig functions (although tends to leave minor artifacts)
evillive2
evillive2
SDL_gfx is extremely simple to use and (no knock on you) will most likely be more efficient than anything you come up with yourself. What parts can't you get working? Does it just not compile on your system? I am sure if you give us some specifics such as platform + compiler + IDE we can help you get it working in no time.
Evillive2
alexmullins
alexmullins
Quote:
Original post by evillive2
SDL_gfx is extremely simple to use and (no knock on you) will most likely be more efficient than anything you come up with yourself. What parts can't you get working? Does it just not compile on your system? I am sure if you give us some specifics such as platform + compiler + IDE we can help you get it working in no time.


i had the same problem. i couldn't get any of the libraries working for me, so i asked a friend and he guided me all the way. Now it works like a charm.
HolyGrail
HolyGrail
Instead of using DrawRect(..) you should lock and unlock the SDL_Surface currently used and address directly the pixels ( surface->pixels).
It's faster :-)
flashingchicken
flashingchicken
i ended up with this, below. when i used sge, i got a weird linker error, SDL_ERROR missing, i think, and for sdl_gfx, it didnt draw anything. my algorithm seems to work pretty well, as long as the radius is <1000 or so
the only problem is that it flats out top, boundry condition or something like that, mess with it later, gonna redo it tomorrow so that it goes top down, instead of side up.
winxp + g++(i think, whatever is the default) + bloodshed
john
void Draw_Circle(SDL_Surface* screen, int cx, int cy, int r, Uint32 color)
{


double theta=0;
int sx=0,sy=0;

do
{
SDL_Rect rect;
sx=(int)(r*cos(theta));
rect.x=cx-sx;
rect.y=cy-sy;
rect.w=sx*2;
rect.h=sy*2;
if (rect.x<0)
{
rect.w-=rect.x;
rect.x=0;
}
if (rect.x+rect.w>SCREEN_W)
{
rect.w=SCREEN_W-rect.x;
}

SDL_FillRect(screen, ▭, color);
sy++;
if (sx!=0)
{theta=atan2( sy,sx);}
else {theta=PI/2.;}

}while (sy<=r);

}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.