# drawing circle in directx

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

## Recommended Posts

how to draw circle in directx ?

##### Share on other sites
Use an alpha texture!

##### Share on other sites
A linestrip with pre-calculated points.

##### Share on other sites
Solid or line circle? Either way just use a textured quad. Use the DirectX SDK Texture Tool to create a .dds file that contains a texture of your circle and an alpha map that shows what parts of the texture are transparent. Load this .dds file to a texture that has the format D3DFMT_A8R8G8B8, enable alpha blending, render and *bam* you have a circle.

##### Share on other sites
Here's what I've done:

//-----------------------------------------------------------------------------------//Draw a simple circle centered around the given point, using the given//radius and color.  This is in Screen Space (2D).//-----------------------------------------------------------------------------------void Draw2DCircle(POINT pt, float radius, D3DCOLOR color, LPDIRECT3DDEVICE9 dev){	const int NUMPOINTS = 24;        const float PI = 3.14159;	D3DTLVERTEX Circle[NUMPOINTS + 1];	int i;	float X;	float Y;	float Theta;	float WedgeAngle;	//Size of angle between two points on the circle (single wedge)	//Precompute WedgeAngle	WedgeAngle = (float)((2*PI) / NUMPOINTS);	//Set up vertices for a circle	//Used <= in the for statement to ensure last point meets first point (closed circle)	for(i=0; i<=NUMPOINTS; i++)	{		//Calculate theta for this vertex		Theta = i * WedgeAngle;				//Compute X and Y locations		X = (float)(pt.x + radius * cos(Theta));		Y = (float)(pt.y - radius * sin(Theta));		Circle = CreateD3DTLVERTEX(X, Y, 0.0f, 1.0f, color, 0.0f, 0.0f);	}	//Now draw the circle        dev->SetFVF(D3DFVF_TL);	dev->SetTexture(0, NULL);	dev->DrawPrimitiveUP(D3DPT_LINESTRIP, NUMPOINTS, &Circle[0], sizeof(Circle[0]));}//Draw2DCircle

Edit - Coder: Used source instead of code tags. The code block is large enough, and it's much more clear now [smile]

[Edited by - Coder on July 27, 2004 11:59:34 AM]

##### Share on other sites
Teric the program got error
i am try to draw circle with wireframe in directx 9
i want the calculation of the circle in directx

##### Share on other sites
What was the error? Yes, my above code draws a wireframe circle (just the circumference, not filled) in DirectX 9.

EDIT: I just looked again, and I saw that there are a couple of things in there that need explanation. First is the CreateD3DTLVERTEX function:
struct D3DTLVERTEX{	float fX;	float fY;	float fZ;	float fRHW;	D3DCOLOR Color;	float fU;	float fV;};//-----------------------------------------------------------------------------------//CreateD3DTLVERTEX--Populate a D3DTLVERTEX structure//-----------------------------------------------------------------------------------D3DTLVERTEX CreateD3DTLVERTEX (float X, float Y, float Z, float RHW, 															 D3DCOLOR color, float U, float V){	D3DTLVERTEX v;	v.fX = X;	v.fY = Y;	v.fZ = Z;	v.fRHW = RHW;	v.Color = color;	v.fU = U;	v.fV = V;	return v;}//CreateD3DTLVERTEX

And the other thing that needs explanation: D3DFVF_TL. This is a #define that I put together:
#define D3DFVF_TL (D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1)

##### Share on other sites
bool mrScreen::DrawCircle (float iCenterX, float iCenterY, float iRadius, Unsigned int iRed, Unsigned int iGreen, Unsigned intiBlue,Unsigned int iAlpha, Unsigned intiVertices){// the Algorithm is simple , a circle is a set of points and has a center and a radius ..etc  Unsigned int iColor;iColor = D3DCOLOR_RGBA (iRed, iGreen, iBlue, iAlpha);Vertex * pkVertices;/* Allocate needed vertices */pkVertices = new Vertex [iVertices + 1];float fAngle = 0;float fComplete;floatiVertex;/* Calculate each vertex position */for (iVertex = 0; iVertex < iVertices; iVertex ++){/* Percentage of circle already drawn */fComplete = (float)iVertex / (float)iVertices;pkVertices [iVertex].m_fX = (float) ((float)iCenterX +((float)iRadius * cos (6.2831f*fComplete)));pkVertices [iVertex].m_fY = (float) ((float)iCenterY +((float)iRadius * sin (6.2831f*fComplete))); // Rx=r*sin(theta)pkVertices [iVertex].m_fZ = 0; // We Are in 2D Space ! pkVertices [iVertex].m_fRHW = 1.0f; // Transformed Vertices pkVertices [iVertex].m_iColor = iColor; // Your Circle Colour/ColorpkVertices [iVertex].m_ftU = 0; // U's Texture Co-ordinatepkVertices [iVertex].m_ftV = 0;// V's Texture Co-ordinate}YourDevice->SetVertexShader (D3DFVF_MIRUSVERTEX);YourDevice->DrawPrimitiveUP (D3DPT_LINESTRIP, iVertices,pkVertices, sizeof (Vertex));  // Draw The Circle! delete [] pkVertices; // After Drawing,Free the Vertex Bufferreturn true;}

Ahmed Saleh.
Edited by Coder: code too wide, used source tags instead.

[Edited by - Coder on July 29, 2004 3:48:35 AM]

##### Share on other sites
Hi Teric,
dev->SetFVF(D3DFVF_TL); /<- Only found in DX9? How about DX8?

Another question to all: How to draw Arc and Eplise in DX8 or above?
Thanks

##### Share on other sites
Quote:
 Original post by OrcishCoderHi Teric,dev->SetFVF(D3DFVF_TL); /<- Only found in DX9? How about DX8?Another question to all: How to draw Arc and Eplise in DX8 or above?Thanks

##### Share on other sites
Quote:
 Original post by OrcishCoderAnother 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 .

##### Share on other sites
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]

##### Share on other sites
hmmm ok, can someone tell me how to put code in the code tag?

thanks.

##### Share on other sites
use the [*source*] tag ;) (without asterisks)

##### Share on other sites
thanks, I was close though :)