Opengl-计算机图形学-实验一
1、资源管理器中创建下拉菜单并添加事件处理程序
- 画直线,中点画线
void CApplicationView::OnZd()
{
CDC *pDC = GetWindowDC();
zhongdian(pDC, 255,455,845,123,RGB(123,234,123));
}
void CApplicationView::zhongdian(CDC *pDC, int x0, int y0, int x1,
int y1, int color)
{
CClientDC dc(this);
int a, b, d, x, y, tag = 0;
if (abs(x1 - x0) < abs(y1 - y0)) {
swap(&x0, &y0);
swap(&x1, &y1);
tag = 1;
}
if (x0 > x1)
{
swap(&x0, &x1);
swap(&y0, &y1);
}
a = y0 - y1;
b = x1 - x0;
d = a + b / 2;
if (y0 < y1)
{
x = x0; y = y0;
//pDC->SetPixel(x, y, color);
Width(pDC, x, y, color, width);
while (x < x1)
{
if (d < 0)
{
x++; y++; d = d + a + b;
}
else
{
x++; d = d + a;
}
if (tag)
// pDC->SetPixel(y, x, color);
Width(pDC, x, y, color, width);
else
Width(pDC, x, y, color, width);
//pDC->SetPixel(x, y, color);
}
}
else {
x = x1; y = y1;
// pDC->SetPixel(x, y, 225);
Width(pDC, x, y, color, width);
while (x > x0)
{
if (d < 0)
{
x--; y++; d = d - a + b;
}
else
{
x--; d = d - a;
}
if (tag)
//pDC->SetPixel(y, x, color);
Width(pDC, x, y, color, width);
else
//pDC->SetPixel(x, y, color);
Width(pDC, x, y, color, width);
}
}
}
2、中点画圆
void CApplicationView::OnCzd()
{
// TODO: 在此添加命令处理程序代码
// TODO: 在此添加命令处理程序代码
CDC *pDC = GetWindowDC();
CZd(pDC, 255,366,100);
}
void CApplicationView::CZd(CDC *pDC, int x0, int y0, int r)
{
int x = 0; int y = r; int d = 1 - r;
Cirpot(pDC, x0, y0, x, y, m_clr);
while (x < y)
{
if (d < 0) d += 2 * x + 3;
else
{
d += 2 * (x - y) + 5;
y--;
}
x++;
Cirpot(pDC, x0, y0, x, y, m_clr);
}
}
void CApplicationView::Cirpot(CDC *pDC, int x0, int y0, int x, int y, int color)
{
pDC->SetPixel((x0 + x), (y0 + y), color);
pDC->SetPixel((x0 + y), (y0 + x), color);
pDC->SetPixel((x0 + y), (y0 - x), color);
pDC->SetPixel((x0 + x), (y0 - y), color);
pDC->SetPixel((x0 - x), (y0 - y), color);
pDC->SetPixel((x0 - y), (y0 - x), color);
pDC->SetPixel((x0 - y), (y0 + x), color);
pDC->SetPixel((x0 - x), (y0 + y), color);
}
3、一只猴头
void CApplicationView::OnLogo3()
{
// TODO: 在此添加命令处理程序代码
CDC *pDC = GetWindowDC();
CPen Pen1(PS_SOLID, 2, RGB(188, 78, 177));
pDC->SelectObject(Pen1);
CBrush brush1(RGB(171, 65, 160));
pDC->SelectObject(brush1);
pDC->Ellipse(500, 300, 700, 500);
pDC->Arc(500, 300, 700, 500, 500, 400, 500, 400);//大圆
pDC->Arc(520, 320, 680, 480, 520, 400, 520, 400);//中间圆
CBrush brush2 RGB(255, 232, 232);
pDC->SelectObject(brush2);
pDC->Ellipse(520, 320, 680, 480);
pDC->Arc(520, 360, 600, 440, 520, 600, 520, 600);//左右圆
pDC->Arc(600, 360, 680, 440, 600, 400, 600, 400);
//眼睛
pDC->SelectObject(brush1);
pDC->Arc(565, 395, 575, 405, 565, 400, 565, 400);
pDC->Arc(625, 395, 635, 405, 625, 400, 625, 400);
pDC->Ellipse(565, 395, 575, 405);
pDC->Ellipse(625, 395, 635, 405);
//耳朵
pDC->Arc(480, 350, 520, 390, 480, 370, 480, 370);
pDC->Arc(680, 350, 720, 390, 680,370, 680, 370);
pDC->Ellipse(680, 350, 720, 390);
pDC->Ellipse(480, 350, 520, 390);
Fill(pDC, 600, 360, RGB(188, 78, 177), RGB(171, 65, 160));
}
//种子填充
void Fill(CDC *pDC,int x,int y, int BoundaryColor, int InteriorColor)
// (x,y) 种子像素的坐标;
// BoundaryColor 边界像素颜色; InteriorColor 需要填充的内部像素颜色
{
if (pDC->GetPixel(x, y) != BoundaryColor && pDC->GetPixel(x, y) != InteriorColor)
// GetPixel(x,y): 返回像素(x,y)颜色
{
pDC->SetPixel(x, y, InteriorColor); // 将像素(x, y)置成填充颜色
Fill(pDC,x+1, y + 1, BoundaryColor, InteriorColor);
Fill(pDC,x-1, y - 1, BoundaryColor, InteriorColor);
Fill(pDC,x - 1, y+1, BoundaryColor, InteriorColor);
Fill(pDC,x + 1, y-1, BoundaryColor, InteriorColor);
}
}