Graphics Reference
In-Depth Information
private
:
float
m
_
center
_
x,
m
_
center
_
y;
// Center of the circle
float
m
_
radius;
// Radius of the circle
};
This is the implementation of the
CCircle2D
class.
.
// constructor and destructor (not shown)
void
CCircle2D::SetCenter(
float
x,
float
y) {
...
}
void
CCircle2D::SetRadius(
float
radius) {
...
}
Draw:
This is the function that approximates a circle by drawing triangles.
void
CCircle2D::
Draw
(
LPDIRECT3DDEVICE9
pDevice)
const
{
// the graphics device and the circle are both defined
if
(m
_
radius <= 0.0f || NULL == pDevice)
return
;
Approximate the circle with 100 triangles (TriangleFan)
DeviceVertexFormat v[100+2];
float
theta = (
float
)((2.0f
*
PI) / 100);
// theta: angle increment for each triangle
v[0].m
_
point = (m
_
center
_
x, m
_
center
_
y);
// Center of the TriangleFan
for
(
int
i=0; i<(kNumPts); i++) {
v[i+1].m
_
point.x = m
_
center
_
x+(m
_
radius
*
sinf(i
*
theta));
v[i+1].m
_
point.y = m
_
center
_
y+(m
_
radius
*
cosf(i
*
theta));
}
v[kNumPts+1] = v[1];
pDevice->DrawPrimitiveUP(D3DPT
_
TRIANGLEFAN, kNumPts, (CONST
void
*
)v,
sizeof
(DeviceVertexFormat));
}
Listing 3.7.
(cont.)
Search WWH ::
Custom Search