用畫點的函式SetPixel實現畫線的功能
阿新 • • 發佈:2019-01-24
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));
}
}
}
{
//如果點陣圖不夠大,則重新申請
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));
}
}
}