Advertisement

Circle filling algorithm?

Started by December 23, 2002 05:39 PM
9 comments, last by FxMazter 22 years, 1 month ago
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

				int nRadie = 50;
				double pi = 3.1415926535;

				for(double v=(pi/180); v <= (2*pi); v+=(pi/180))
				{
					for(int z=1; z<=(nRadie); z++)
					{
						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]
int y = (int)((z)/(sin(v)))+100;

should be

int y = (int)(z*sin(v))+100;
Advertisement
Yeh thats right!
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]
Lewis [m80]Interactive Designerhttp://ismstudios.com
I do have a forum question... how do you get that syntax highlighted box for your code??????
Lewis [m80]Interactive Designerhttp://ismstudios.com
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....
Advertisement

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.
Yes, that it is a good idee to only paint one seg and flip to achieve the rest thats like 4 times faster
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.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
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]

This topic is closed to new replies.

Advertisement