# Code for filled circle

Below is code that draws a circle I got it from
http://www.ericgiguere.com/microjava/cldc_circle.html

void draw_circle(int xC, int yC, int r)
{
int x=0, y=r,u=1,v=2*r-1,E=0;
while(x < y)
{
set_pixel(xC+x, yC+y);
set_pixel(xC+y, yC-x);
set_pixel(xC-x, yC-y);
set_pixel(xC-y, yC+x);
x++; E+= u; u+=2;
if (v < 2 * E)
{
y–;
E -= v;
v -= 2;
}
if (x > y)
break;
set_pixel(xC+y, yC+x);
set_pixel(xC+x, yC-y);
set_pixel(xC-y, yC-x);
set_pixel(xC-x, yC+y);
}
}

if I call draw circle like this
for (int i=1;i<=15;i++)
draw(200,200,i);

I attempt to draw a filled circle with radius of 15 but does not completly fill
the circle. Does anybody have sample code that draws a filled circle?
Todd V. Rovito
@Todd_V_Rovito
Carpe Aptenodytes! ================ “Seize the Penguins!”

Check this out: http://freshmeat.net/projects/sdl_gfxprimitives/
It’s an SDL-based library for drawing several primitives, including filled
circles. Source code included.

Mahmoud Al Gammal>Below is code that draws a circle I got it from

http://www.ericgiguere.com/microjava/cldc_circle.html

void draw_circle(int xC, int yC, int r)
{
int x=0, y=r,u=1,v=2*r-1,E=0;
while(x < y)
{
set_pixel(xC+x, yC+y);
set_pixel(xC+y, yC-x);
set_pixel(xC-x, yC-y);
set_pixel(xC-y, yC+x);
x++; E+= u; u+=2;
if (v < 2 * E)
{
y–;
E -= v;
v -= 2;
}
if (x > y)
break;
set_pixel(xC+y, yC+x);
set_pixel(xC+x, yC-y);
set_pixel(xC-y, yC-x);
set_pixel(xC-x, yC+y);
}
}

if I call draw circle like this
for (int i=1;i<=15;i++)
draw(200,200,i);

I attempt to draw a filled circle with radius of 15 but does not completly
fill
the circle. Does anybody have sample code that draws a filled circle?

Todd V. Rovito
rovitotv at stargt.com
Carpe Aptenodytes! ================ “Seize the Penguins!”

SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

[…]

if I call draw circle like this
for (int i=1;i<=15;i++)
draw(200,200,i);

Well, that’s about the slowest way you can possibly render a filled
circle!

How about calculating the coordinates of the left and right edges (using
sin() is the easiest way, although there are faster ways), and then do
the rendering using SDL_FillRect()…?

Something like

#include <math.h>
void circle(SDL_Surface screen, int x, int y, int r, Uint32 color)
{
int y1, y2;
SDL_Rect rect;
for(y1 = -r, y2 = r; y1; ++y1, --y2)
{
int xr = (int)(sqrt(r
r - y1*y1) + 0.5);
rect.x = x - xr;
rect.y = y + y1;
rect.w = 2 * xr;
rect.h = 1;
SDL_FillRect(screen, &rect, color);
rect.y = y + y2;
rect.h = 1;
SDL_FillRect(screen, &rect, color);
}
rect.x = x - r;
rect.y = y;
rect.w = 2 * r;
rect.h = 1;
SDL_FillRect(screen, &rect, color);
}

You’d probably want to replace the sqrt() with some fixed point
approximation for speed. (A 3rd or 4th degree polynomial or something. In
iterative code like this, they can be imlemented using only adds.)

BTW, how come the docs don’t mention that SDL_FillRect() writes the
resulting clipped rectangle into the dest rect?

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
`----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |`-------------------------------------> http://olofson.net -'On Sunday 30 December 2001 23:31, Todd V . Rovito wrote:

[…]

if I call draw circle like this
for (int i=1;i<=15;i++)
draw(200,200,i);

Well, that’s about the slowest way you can possibly render a filled
circle!

How about calculating the coordinates of the left and right edges
(using sin() is the easiest way, although there are faster ways), and

Uhm, as you might have noticed, sin() does not work with this algorithm!
I wasn’t thinking straight when I wrote that - but it seems like I was
when I hacked the example code…

``````  int xr = (int)(sqrt(r*r - y1*y1) + 0.5);
``````

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
`----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |`-------------------------------------> http://olofson.net -'On Tuesday 08 January 2002 00:32, David Olofson wrote:

On Sunday 30 December 2001 23:31, Todd V . Rovito wrote: