一、實驗目的
1.掌握在MFC中搭建圖形繪制的基本框架的方法;
2.將直線的中點Bresenham算法轉化成可執行代碼。
二、實驗內容
1. 通過分析具體數據在中點Bresenham算法上的執行過程,繪制算法執行流程圖或N-S圖,在MFC中實現該算法,要求編寫函數實現任意給定兩點繪制線段。
三、實驗步驟
任意給定的兩點所繪制的線段斜率k可能有四種情況,分別是:0<k<1,k>=1,-1<k<0,
k<=-1。下面對這四種情況分別進行分析。
(一) 當0<k<1時
1.算法原理的推導
(1)構造中點誤差項為:

(2)中點誤差的初始值是:

(3)推導di+1


2.算法執行的N-S圖

3.算法執行的主要代碼
1 void CExp2View::OnLButtonUp(UINT nFlags, CPoint point) 2 3 { 4 5 // TODO: Add your message handler code here and/or call default 6 7 p1=point; 8 9 CDC *pDC=this->GetDC(); 10 11 COLORREF c; 12 13 DrawLine(pDC,p0,p1,c); 14 15 CView::OnLButtonUp(nFlags, point); 16 17 } 18 19 20 21 void CExp2View::OnLButtonDown(UINT nFlags, CPoint point) 22 23 { 24 25 // TODO: Add your message handler code here and/or call default 26 27 p0=point; 28 29 CView::OnLButtonDown(nFlags, point); 30 31 } 32 33 34 35 void CExp2View::DrawLine(CDC *pDC, CPoint p0, CPoint p1,COLORREF c) 36 37 { ///* 38 39 //1.fabs(k)>0&&fabs(k)<1 40 41 double k=1.0*(p1.y-p0.y)/(p1.x-p0.x), d=0.5-k; 42 43 if(fabs(k)>1) return; 44 45 int x,y; 46 47 for(x=p0.x,y=p0.y;x<=p1.x;x++){ 48 49 if(d>=0){ 50 51 pDC->SetPixel(x,y,0xff0000); 52 53 d+=-k; 54 55 } 56 57 else{ 58 59 y++; 60 61 pDC->SetPixel(x,y,0xff0000); 62 63 d+=1-k; 64 65 } 66 67 } 68 69 //*/ 70 71 }
4.執行結果
![]() |
(二) 當k>=1時
1.算法原理的推導
(1)構造中點誤差項為:

(2)中點誤差的初始值是:

(3)推導di+1


2.算法執行的N-S圖

3.算法執行的主要代碼
1 void CExp2View::OnLButtonUp(UINT nFlags, CPoint point) 2 3 { 4 5 // TODO: Add your message handler code here and/or call default 6 7 p1=point; 8 9 CDC *pDC=this->GetDC(); 10 11 COLORREF c; 12 13 DrawLine(pDC,p0,p1,c); 14 15 CView::OnLButtonUp(nFlags, point); 16 17 } 18 19 20 21 void CExp2View::OnLButtonDown(UINT nFlags, CPoint point) 22 23 { 24 25 // TODO: Add your message handler code here and/or call default 26 27 p0=point; 28 29 CView::OnLButtonDown(nFlags, point); 30 31 } 32 33 34 35 void CExp2View::DrawLine(CDC *pDC, CPoint p0, CPoint p1,COLORREF c) 36 37 { ///* 38 39 //2.fabs(k)>=1 40 41 double k=1.0*(p1.y-p0.y)/(p1.x-p0.x), d=1-0.5*k; 42 43 if(fabs(k)>0&&fabs(k)<1) return; 44 45 int x,y; 46 47 for(x=p0.x,y=p0.y;x<=p1.x;y++){ 48 49 if(d>=0){ 50 51 x++; 52 53 pDC->SetPixel(x,y,0x00ff00); 54 55 d+=1-k; 56 57 } 58 59 else{ 60 61 62 63 pDC->SetPixel(x,y,0x00ff00); 64 65 d+=1; 66 67 } 68 69 } 70 71 }
4.執行結果
(三) 當-1<k<0時
1.算法原理的推導
(1)構造中點誤差項為:

(2)中點誤差的初始值是:

(3)推導di+1


2.算法執行的N-S圖

3.算法執行的主要代碼
1 void CExp2View::OnLButtonUp(UINT nFlags, CPoint point) 2 3 { 4 5 // TODO: Add your message handler code here and/or call default 6 7 p1=point; 8 9 CDC *pDC=this->GetDC(); 10 11 COLORREF c; 12 13 DrawLine(pDC,p0,p1,c); 14 15 CView::OnLButtonUp(nFlags, point); 16 17 } 18 19 20 21 void CExp2View::OnLButtonDown(UINT nFlags, CPoint point) 22 23 { 24 25 // TODO: Add your message handler code here and/or call default 26 27 p0=point; 28 29 CView::OnLButtonDown(nFlags, point); 30 31 } 32 33 34 35 void CExp2View::DrawLine(CDC *pDC, CPoint p0, CPoint p1,COLORREF c) 36 37 { ///* 38 39 /* 40 41 //3.fabs(k)>-1&&fabs(k)<0 42 43 double k=1.0*(p1.y-p0.y)/(p1.x-p0.x), d=-0.5-k; 44 45 //if(fabs(k)>1||fabs(k)<-1||(fabs(k)>0&&fabs(k)<1)) return; 46 47 int x,y; 48 49 for(x=p0.x,y=p0.y;x<=p1.x;x++){ 50 51 if(d>=0){ 52 53 y=y-1; 54 55 pDC->SetPixel(x,y,0x0000ff); 56 57 d+=-1-k; 58 59 } 60 61 else{ 62 63 pDC->SetPixel(x,y,0x0000ff); 64 65 d+=-k; 66 67 } 68 69 } 70 71 */}
4.執行結果
![]() |
(四) 當k<=-1時
1.算法原理的推導
(1)構造中點誤差項為:

(2)中點誤差的初始值是:

(3)推導di+1


2.算法執行的N-S圖

3.算法執行的主要代碼
1 void CExp2View::OnLButtonUp(UINT nFlags, CPoint point) 2 3 { 4 5 // TODO: Add your message handler code here and/or call default 6 7 p1=point; 8 9 CDC *pDC=this->GetDC(); 10 11 COLORREF c; 12 13 DrawLine(pDC,p0,p1,c); 14 15 CView::OnLButtonUp(nFlags, point); 16 17 } 18 19 20 21 void CExp2View::OnLButtonDown(UINT nFlags, CPoint point) 22 23 { 24 25 // TODO: Add your message handler code here and/or call default 26 27 p0=point; 28 29 CView::OnLButtonDown(nFlags, point); 30 31 } 32 33 34 35 void CExp2View::DrawLine(CDC *pDC, CPoint p0, CPoint p1,COLORREF c) 36 37 { ///* 38 39 //1.fabs(k)>0&&fabs(k)<1 40 41 double k=1.0*(p1.y-p0.y)/(p1.x-p0.x), d=0.5-k; 42 43 if(fabs(k)>1) return; 44 45 int x,y; 46 47 for(x=p0.x,y=p0.y;x<=p1.x;x++){ 48 49 if(d>=0){ 50 51 pDC->SetPixel(x,y,0xff0000); 52 53 d+=-k; 54 55 } 56 57 else{ 58 59 y++; 60 61 pDC->SetPixel(x,y,0xff0000); 62 63 d+=1-k; 64 65 } 66 67 } 68 69 //*/ 70 71 }
4.執行結果
![]() |
四、實驗結果與討論
根據任意給定的兩點所繪制的線段斜率k可能有的四種情況,實驗結果如下:
(一) 當0<k<1時:
![]() |
(二) 當k>=1時:
![]() |
(三) 當-1<k<0時:
![]() |
(四) 當k<=-1時:
![]() |
五、總結
(一)本次實驗按時按量完成。
(二)通過本次實驗,我掌握了在MFC中搭建圖形繪制的基本框架的方法;掌握了如何將直線的中點Bresenham算法轉化成可執行代碼。
(三)在本次實驗中,我通過分析具體數據在中點Bresenham算法上的執行過程,分四種情況繪制算法執行N-S圖,並且在MFC中實現了該算法。
參見源碼:https://github.com/shenxiaolinZERO/Resources/tree/master/Resources/Computer-Graphics/Bresenham







