Archived

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

farmersckn

drawing a circle

Recommended Posts

farmersckn    122
how do you draw a circle, without using cos, sin etc. this is what i have so far, but it only draws a vertical line. btw, got it from pcgpe.
        
void CSurface::Circle (int iDestX1, int iDestY1, int iRadius, byte bColor)
{
	int iX1;
	int iY1;
	int iDecision;

	iDecision = 3 - (2 * iRadius);
	iY1 = iRadius;
	iX1 = 0;

	while (iX1 != iY1)
	{
		lpSurfaceData[iDestX1 + iX1 + (iDestY1 + iY1) * iWidth] = bColor;
		lpSurfaceData[iDestX1 + iX1 + (iDestY1 - iY1) * iWidth] = bColor;
		lpSurfaceData[iDestX1 - iX1 + (iDestY1 + iY1) * iWidth] = bColor;
		lpSurfaceData[iDestX1 - iX1 + (iDestY1 - iY1) * iWidth] = bColor;
		
		if (iDecision < 0)
		{
			iDecision += 4 * (iX1 + 6);
		}
		else
		{
			iDecision += 4 * (iX1 - iY1) + 10;
			iY1--;
		}
	}
}
        
thanks in advance. farmersckn Edited by - farmersckn on 10/4/00 4:49:58 PM

Share this post


Link to post
Share on other sites
Dark Star    100
Well I don't know about that code but. what's wrong with using sin and cos. if you can make sin and cos look up tables then there will be no speed problems in your game or program

this is How i draw a circle

    
for (int a=0; a<360; a++)
{
int x=(int)COS<A href='http://*radius; // SIN and COS are arrays

int y=(int)SIN[a]*radius; // of sin/cos in degrees

Plot_Pixel (x,y,colour);
}


I cannot remember how to create the SIN/COS tables off the top of my head but u have to convert the angles to degrees because sin and cos functions in C give results in radians. Coding it your way make it harder. just use sin and cos. It won't hurt


Dark Star


Sorry about the code, this board mucked it up



Edited by - Dark Star on October 4, 2000 9:15:40 AM

Edited by - Dark Star on October 4, 2000 9:16:41 AM ' Target=_Blank>Link

Share this post


Link to post
Share on other sites
farmersckn    122
the only thing is that i think this other method is faster, besides, if the circle being drawn is big enough, you will have gaps because there will be more than 360 pixels in the circumference. sin and cos are float values the first way uses ints only. thank you very much for the help, though.

Edited by - farmersckn on October 4, 2000 5:54:30 PM

Share this post


Link to post
Share on other sites
MSkinn99    122
HMMMMMMMM...

I seem to remember seeing a fast circle algorithm based on Besenhams line algo...I''ll let u know when i find it...

MSkinn99

What's done is done (Until you hit Undo) :)

Share this post


Link to post
Share on other sites
MSkinn99    122
OK, found it:

void circle(int seg, int off, int xc, int yc, int r, int c, int fillit)

seg=segment of destination
off=offset of destination
xc=x coordinate
yc=y coordinate
r=radius
c=color
fillit=fill or not(0=empty, 1=filled)0

example: circle(0xA000, 0, 160, 100, 50, 4, 0)

OK...now here is the code for the algorithm:

    
void circle(int seg, int off, int xc, int yc, int r, int c, int fillit)
{
//Bresenham Circle Drawing Algorithm

int x, d, w;
x = 0; d = 2 * (1 - r); w = 2 * 320 / 200;
while (r >= 0)
{
if (fillit == 1)
{
line(seg, off, xc+x,yc+r,xc+x,yc-r,c);
line(seg, off, xc-x,yc+r,xc-x,yc-r,c);
}
else
{
pset (seg, off, xc + x, yc + r, c);
pset (seg, off, xc + x, yc - r, c);
pset (seg, off, xc - x, yc + r, c);
pset (seg, off, xc - x, yc - r, c);
}
if ((d + r) > 0)
{
r--;
d = d - w * r - 1;
}
if (x > d)
{
x++;
d = d + 2 * x + 1;
}
}
}


pset()=pixel setting function(outline only)
line()=line drawing function(solid only)

hope it's useful...enjoy!

MSkinn99

What's done is done (Until you hit Undo)

Edited by - MSkinn99 on October 4, 2000 6:25:16 PM

Edited by - MSkinn99 on October 4, 2000 6:26:36 PM

Edited by - MSkinn99 on October 4, 2000 6:27:16 PM

Share this post


Link to post
Share on other sites
farmersckn    122
an ellipse would be the same as circle, except that for every time you draw the pixels, you mult x and y by their respective x and y aspect ratios.
all that means, is that, notice the w in the function for the circle? take it out of the equation in the while loop and watch the result. notice that the circle is no longer a "circle" on the screen. because the screen does not have square pixels, it appears to be a stretched circle, or ellipse. by multiplying the y value in the while loop, you cause the pixels to be drawn a little "off" from the "true" circle, and that makes it "look" like a true circle on screen. that''s how you do the ellipse. you take the amount you want to stretch the x and y size of the circle and mult inside that while loop. or, to squash, like we do to draw the circle, you would find a value just smaller than 1.0 to mult by. hope that helps.

Share this post


Link to post
Share on other sites
Knarkles    271
quote:
Original post by farmersckn

an ellipse would be the same as circle, except that for every time you draw the pixels, you mult x and y by their respective x and y aspect ratios.


Ah, of course, stupid me. How about a rotated ellipse? Hmm...

quote:
all that means, is that, notice the w in the function for the circle? take it out of the equation in the while loop and watch the result. notice that the circle is no longer a "circle" on the screen. because the screen does not have square pixels, it appears to be a stretched circle, or ellipse.


640x480, 800x600 etc. have square pixels, 320x200 and 640x400 don''t.

-Jussi

Share this post


Link to post
Share on other sites