赞
踩
讨论如何用d3d9来绘制圆及简单的圆角矩形。
画圆时采用Bresenham算法。不失一般性,假设圆的圆心位于坐标原点(如果圆心不在原点,可以通过坐标平移使其与原点重合),半径为R。以原点为圆心的圆C有四条对称轴:x=0,y=0,x=y和x=-y。若已知圆弧上一点P1=C(x, y),利用其对称性便可以得到关于四条对称轴的其它7个点,即: P2=C(x,-y), P3=C(-x, y), P4=C(-x,-y), P5=C(y,x), P6=C(-y,x), P7=C(y,-x), P8=C(-y,-x)。
这种性质称为八对称性。因此,只要扫描转换八分之一圆弧,就可以通过圆弧的八对称性得到整个圆。
我们以(0,0)为原点,r为半径,坐标系xy方向与屏幕坐标系一致,计算y轴正向右侧的八分之一圆弧,其它圆弧通过对称性得到。
顶点格式采用如下定义:
- struct SCREEN_VERTEX_UNTEX
-
- {
-
- float x, y, z, h;
-
- D3DCOLOR color;
-
-
-
- static DWORD FVF;
-
- };
-
- SCREEN_VERTEX_UNTEX::FVF = D3DFVF_XYZRHW | D3DFVF_DIFFUSE
下面是画圆函数:
- void DrawCircle( IDirect3DDevice9* pd3dDevice, int xCenter, int yCenter, int nRadius, D3DCOLOR FrameColor)
-
- {
-
- SCREEN_VERTEX_UNTEX *pVertices = new SCREEN_VERTEX_UNTEX[2 * D3DX_PI * nRadius];
-
- //Bresenham algorithm
-
- int x=0, y=nRadius, d=1-nRadius, i=0;
-
- while(x <= y)
-
- {
-
- //get eight points
-
- //(x,y)
-
- pVertices[i].x = x + xCenter;
-
- pVertices[i].y = y + yCenter;
-
- pVertices[i].z = 0.5f;
-
- pVertices[i].h = 1.0f;
-
- pVertices[i].color = FrameColor;
-
- //(x,-y)
-
- ++i;
-
-
-
- pVertices[i].x = x + xCenter;
-
- pVertices[i].y = -y + yCenter;
-
- pVertices[i].z = 0.5f;
-
- pVertices[i].h = 1.0f;
-
- pVertices[i].color = FrameColor;
-
- //(-x, y)
-
- ++i;
-
- pVertices[i].x = -x + xCenter;
-
- pVertices[i].y = y + yCenter;
-
- pVertices[i].z = 0.5f;
-
- pVertices[i].h = 1.0f;
-
- pVertices[i].color = FrameColor;
-
- //(-x, -y)
-
- ++i;
-
- pVertices[i].x = -x + xCenter;
-
- pVertices[i].y = -y + yCenter;
-
- pVertices[i].z = 0.5f;
-
- pVertices[i].h = 1.0f;
-
- pVertices[i].color

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。