图形学作业:五角星VC++画法
void CHuayuView::OnDraw(CDC* pDC)
{
CHuayuDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//画五角星
int X0=100,Y0=100,R=80;
float a[6][3],b[6][3];
float xr;
float pi=3.14;
a[1][1] = X0; a[1][2] = Y0 + R;
a[2][1] = X0 + R * sin(pi / 2.5);
a[2][2]= Y0 + R * cos(pi / 2.5);
a[3][1] = R * sin(pi / 5) + X0;
a[3][2] = Y0 - R * cos(pi / 5);
a[4][1] = -R * sin(pi / 5) + X0;
a[4][2] =a[3][2];
a[5][1] = X0 - R * sin(pi / 2.5);
a[5][2] = a[2][2];
xr = (a[2][2] - Y0) / cos(pi / 5);
b[1][1] = X0 - sin(pi / 5) * xr;
b[1][2] = a[2][2];
b[2][1] = X0 + sin(pi / 5) * xr;
b[2][2] = b[1][2];
b[3][1] = sin(pi / 2.5) * xr + X0;
b[3][2] = -cos(pi / 2.5) * xr + Y0;
b[4][1] = X0; b[4][2] = -xr + Y0;
b[5][1] = -sin(pi / 2.5) * xr + X0;
b[5][2] = b[3][2];
pDC->MoveTo(a[5][1], a[5][2]);
int i;
for(i=1;i<6;i++)
{
pDC->LineTo(b[i][1], b[i][2]);
pDC->LineTo(a[i][1], a[i][2]);
}
pDC->MoveTo(a[1][1], a[1][2]);pDC->LineTo(b[4][1], b[4][2]);
pDC->MoveTo(a[2][1], a[2][2]);pDC->LineTo(b[5][1], b[5][2]);
pDC->MoveTo(a[3][1], a[3][2]);pDC->LineTo(b[1][1], b[1][2]);
pDC->MoveTo(a[4][1], a[4][2]);pDC->LineTo(b[2][1], b[2][2]);
pDC->MoveTo(a[5][1], a[5][2]);pDC->LineTo(b[3][1], b[3][2]);
int c[5][2];
for(i=0;i<5;i++)
{c[i][0]=(a[i+1][1]+b[i+1][1]+X0)/3;
c[i][1]=(a[i+1][2]+b[i+1][2]+Y0)/3;
}
int d[5][2];
for(i=0;i<5;i++)
{d[i][0]=(a[i+1][1]+b[i+2][1]+X0)/3;
d[i][1]=(a[i+1][2]+b[i+2][2]+Y0)/3;
}
d[4][0]=(a[5][1]+b[1][1]+X0)/3;
d[4][1]=(a[5][2]+b[1][2]+Y0)/3;
CBrush brush;
brush.CreateSolidBrush(RGB(0,255,0));
pDC->SelectObject(&brush);
for(i=0;i<5;i++)
{pDC->ExtFloodFill(c[i][0],c[i][1],RGB(0,0,0),FLOODFILLBORDER);//在指定的区域内填充颜色
Sleep(40);
}
CBrush brush2;
brush2.CreateSolidBrush(RGB(222,111,222));
pDC->SelectObject(&brush2);
for(i=0;i<5;i++)
{pDC->ExtFloodFill(d[i][0],d[i][1],RGB(0,0,0),FLOODFILLBORDER);
Sleep(40);
}
}
1