用画点的函数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
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值