# 用画点的函数SetPixel实现画线的功能

C++面向对象编程 专栏收录该内容
77 篇文章 0 订阅
void CDlgMain::UpdateDraw(CDC* pDC, CRect rect)
{
//如果位图不够大，则重新申请
if( m_bitmap.m_hObject )
{
BITMAP info;
m_bitmap.GetBitmap( &info );
//释放原来的小位图
if( info.bmWidth<rect.Width() || info.bmHeight<rect.Height() )
m_bitmap.DeleteObject();
}

// pDC->FillRect(rect, &CBrush(RGB(0, 255, 0)));

// int i, j;
// double a, b;
// a = 5;
//
// for (i=0; i<50; i++)
// {
// b = (int)sqrt(a);
// j = b * i;
//
// pDC->SetPixel(i, j, RGB(255, 0, 0));
// }

MoveDraw(100, 50, 300, 1);

//   pDC->MoveTo(100, 50);
//   pDC->LineTo(300, 1);

pDC->BitBlt( rect.left, rect.top, rect.Width(), rect.Height(), pDC, rect.left, rect.top, SRCCOPY );
}

BOOL CDlgMain::OnEraseBkgnd(CDC* pDC)
{
// CRect rc;
// GetClientRect(rc);
// pDC->FillRect(rc, &CBrush(RGB(0, 0, 255)));
return CDialog::OnEraseBkgnd(pDC);
}

void CDlgMain::MoveDraw(int x1, int y1, int x2, int y2)
{
CDC* pDC = GetDC();

int i, j;
double x, y, tanValue;

//求绝对值:int abs(int x);
x = abs(int (x2-x1));
y = abs(int (y2-y1));

/*****************************************/
//1. x1<x2; y1<y2;
//2. x1>x2; y1<y2;
//3. x1<x2; y1>y2;
//4. x1>x2; y1>y2;

//5. x1=x2; y1<y2;
//6. x1=x2; y1>y2;
//7. x1<x2; y1=y2;
//8. x1>x2; y1=y2;
//
/*****************************************/

// /*1. x1<x2; y1<y2;************************/
if (x1<x2 && y1<y2)
{
for (i=0; i<x; i++)
{
//tan(角度)  tanValue = y/x;  y = tanValue * x;
//cot(角度)  cotValue = x/y;  x = cotValue * y;
tanValue = y/x;

x2 = i + x1;
y2 = i*tanValue + y1;

//pDC->SetPixel(i , i*cot(X), RGB(255, 0, 0));
//pDC->SetPixel(x, x*cotValue, RGB(255, 0, 0));
pDC->SetPixel(x2, y2, RGB(255, 0, 0));
}

}
/*2. x1>x2; y1<y2;*****************************/
else if (x1>x2 && y1<y2)
{
for (i=0; i<x; i++)
{
tanValue = y/x;

x2 = x1 - i;
y2 = y1 + i*tanValue;

pDC->SetPixel(x2, y2, RGB(255, 0, 0));
}
}
/*3. x1<x2; y1>y2;*************************/
else if (x1<x2 && y1>y2)
{
for (i=0; i<x; i++)
{
tanValue = y/x;

x2 = x1 + i;
y2 = y1 - i*tanValue;

pDC->SetPixel(x2, y2, RGB(255, 0, 0));
}

}
/*4. x1>x2; y1>y2;*************************/
else if (x1>x2 && y1>y2)
{
for (i=0; i<x; i++)
{
tanValue = y/x;

x2 = x1 - i;
y2 = y1 - i*tanValue;

pDC->SetPixel(x2, y2, RGB(255, 0, 0));
}

}
/*5  x1<x2; y1=y2;**********************************/
else if (x1<x2 &&  y1==y2)
{
for (i=0; i<x; i++)
{
//3. x1<x2; y1=y2;
x2 = x1 + i;
y2 = y1;

pDC->SetPixel(x2, y2, RGB(255, 0, 0));
}

/*6. x1>x2; y1=y2;***********************************/
else if ( x1>x2 && y1==y2)
{
for (i=0; i<x; i++)
{
//4. x1>x2; y1=y2;
x2 = x1 - i;
y2 = y1;

pDC->SetPixel(x2, y2, RGB(255, 0, 0));
}
}
/*7. x1=x2; y1<y2;****************************************/
else if (x1==x2 && y1<y2)
{
for (i=0; i<y; i++)
{
//1. x1=x2; y1<y2;
x2 = x1;
y2 = i + y1;

pDC->SetPixel(x2, y2, RGB(255, 0, 0));
}
}
/*8. x1=x2; y1>y2;***********************************/
else if (x1==x2 && y1>y2)
{
for (i=0; i<y; i++)
{
//2. x1=x2; y1>y2;
x2 = x1;
y2 = y1 - i;

pDC->SetPixel(x2, y2, RGB(255, 0, 0));
}
}

}
• 1
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

12-01
12-13

03-18 6697
12-06 1万+
11-20 4625
07-06 8540
05-13 493
03-09 915