#### Archived

This topic is now archived and is closed to further replies.

# Circle filling algorithm?

This topic is 5493 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello! I made an algorithm for drawing and filling a circle with pixels. But hence, it doesn't work at all... the pixels get spread all over the screen. Here is how it looks:


//Algorith for plotting a circle filled with pixels

double pi = 3.1415926535;

for(double v=(pi/180); v <= (2*pi); v+=(pi/180))
{
{
int y = (int)((z)/(sin(v)))+100;
int x = (int)((cos(v))*z)+100;

Plot32BitPixel(x, y, color, pixelBuffer, nPitch);
}
}


Ok, I'm no genious in neighter programming nor maths, so thes could probably be done much better but, shouldn't I at least get somewhat a circular object painted with this?? as you can see, the v is incremented with 1 degree for 360 degrees but is written in radians. Can anyone figure out why this is so TOTALLY wrong? I have tried to make this algorith according to these mathematical laws:

sin v = a/c
cos v = b/c

y-coordinate

^
|   /|
| c/ |a
| /  |
|/v__|___ >    x-coordinate
b

a, b, c = lenght of the sides on the triangle
v = degrees between y = 0 and the current y value
(triangle in a Coordinate system)

THX! [edited by - FxMazter on December 23, 2002 6:51:34 PM] [edited by - FxMazter on December 23, 2002 6:55:27 PM] [edited by - FxMazter on December 23, 2002 6:57:26 PM] [edited by - FxMazter on December 23, 2002 6:58:17 PM] [edited by - FxMazter on December 23, 2002 7:00:32 PM] [edited by - FxMazter on December 23, 2002 7:02:13 PM]

##### Share on other sites
int y = (int)((z)/(sin(v)))+100;

should be

int y = (int)(z*sin(v))+100;

Yeh thats right!

##### Share on other sites
I was thinking that you could "cheat" since you are using pixels to draw this filled circle...

If you know your radius, you can then actually draw a quarter of a circle while mirroring it to it''s other qaudrants. You can do this with any segement, but since we are concerned with speed, we''ll stick to 3 mirrors.

Think about it... Let''s work out a section of a semi circle. Let''s find the point to a 45 degree angle on the circle. Easy enough? We could also used a lookup table in this case. Let''s also attain the other quarter of the semi by retaining our y value and doubling and negating the x axis. We now have two points to draw a horizontal line to.

You don''t want gaps in the circle depending on the size of it and the accurateness of the functions so you can avoid this by... drawing and mirroring a triangle with 30 degrees. If you want a really big circle for really huge resolutions, try 15 degrees. Even @ 15 degrees, you will be drawing ONLY 6 TRIANGLES per quadrant. It will cost you almost nothing to do the simple mirror operations.

These are just suggestions and I hope they make sense. I like attacking problems from different angles because I like my clock cycles!!!

-Lewie [m80]

##### Share on other sites
I do have a forum question... how do you get that syntax highlighted box for your code??????

##### Share on other sites
quote:
Original post by LewieM80
I do have a forum question... how do you get that syntax highlighted box for your code??????

Hit the edit button for his post....

##### Share on other sites

You can draw a circle with 8 point symmetry, not just 4. So you only would really need to calculate 45 degrees of the circle - you can mirror\flip to get all the other segments.

##### Share on other sites
Yes, that it is a good idee to only paint one seg and flip to achieve the rest thats like 4 times faster

##### Share on other sites
quote:
Original post by LewieM80
I do have a forum question... how do you get that syntax highlighted box for your code??????

There is a general forum FAQ that gives some hints about formatting. Hit the "faq" link at the upper right of this screen.

But to answer your question, just surround the code with "[source ]" at the top and "[/source ]" at the bottom. And remove the space before "]".

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

##### Share on other sites
I just can't stand to see circles being drawn like that. It's so slow! Plus it leaves gaps in big circles. Try this:

    for(y=0;y<nRadie;y++){	int width=sqrt(nRadie*nRadie-y*y);// find width of circle at this y	for(x=0;x<width;x++) //draw a horizontal line of that width	{		Plot32BitPixel(100+x, 100+y, color, pixelBuffer, nPitch);		Plot32BitPixel(100+x, 100-y, color, pixelBuffer, nPitch);		Plot32BitPixel(100-x, 100+y, color, pixelBuffer, nPitch);		Plot32BitPixel(100-x, 100-y, color, pixelBuffer, nPitch);	}}

[edited by - Bagel Man on January 4, 2003 2:17:15 AM]

for(int h = radius; h >= -radius; --h){   int w = 2 * sqrt( radius*radius - h*h );   int local_x = X - (w / 2);   int local_y = Y - h;   Rectangle(local_x, local_y, w, 1, color_r, color_g, color_b, color_a);}
Rectangle is such a trivial function to code, I''m sure you can handle that If you want an unfilled circle, then simply draw a pixel at the start and end points.