drawing circle in directx

Started by
13 comments, last by nhatkthanh 19 years, 8 months ago
Quote:Original post by OrcishCoder
Another question to all: How to draw Arc and Eplise in DX8 or above?
Thanks

An arc is a portion of a circle or elipse. An elipse is a circle with the radius biased in either the x- or y-axis.

To draw an elipse, bias like this:

pkVertices [iVertex].m_fX = (float) ((float)iCenterX +((float)iRadius * xBias * cos (6.2831f*fComplete)));pkVertices [iVertex].m_fY = (float) ((float)iCenterY +((float)iRadius * yBias * sin (6.2831f*fComplete)));


To draw an arc, only calculate vertices in the area you need (ie, calculate positions in the loop for only a subset of those in the circle)

Or you could do everything my way and precalculate everything so you don't have to worry about the math .
Advertisement
look up for Bresenham's circle algorithm. You basically draw 1/4 of the circle and flip it 3 more times to complete the circle. This is the code that I wrote a while back for a class, just replace the drawpixel routine to a specific API to plot a pixel for a given screen coordinate.

attempting to post code.
[SOURCE]//Bresenham's circle algorithmvoid drawCircle (){	int tx,ty;	int xp = 0;	int yp = rad;	//swap(centerX,centerY);	double d = 1-rad;	while(xp <= yp)	{				tx = xp+centerX;		ty = yp+centerY;        		//second octant		drawPixle(tx,ty,1);		//third octant		drawPixle(xp-2*xp+centerX,ty,1);		//7th octant		drawPixle(tx,yp-2*(yp)+centerY,1);		//first octant		drawPixle(yp+centerX,xp+centerY,1);		//8th octant		drawPixle(yp+centerX,xp-2*xp+centerY,1);		//sixth octant		drawPixle(-1*xp+centerX,-yp+centerY,1);		//4th octant		drawPixle(-yp+centerX,(-xp+2*xp)+centerY,1);		//5th octant		drawPixle(-yp+centerX,-xp+centerY,1);		//go E		if(d < 0.0)		{			d = d + 2*xp + 3;			xp = xp+1;		}		else	//go SE		{			d = d + 2*xp - 2*yp + 5;			xp = xp+1;			yp = yp - 1;		}	}}[/SOURCE]
hmmm ok, can someone tell me how to put code in the code tag?

thanks.
use the [*source*] tag ;) (without asterisks)
thanks, I was close though :)

This topic is closed to new replies.

Advertisement