zoukankan      html  css  js  c++  java
  • 直线的中点Bresenham算法的实现

    一、实验目的

    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

  • 相关阅读:
    codeforces 455B A Lot of Games(博弈,字典树)
    HDU 4825 Xor Sum(二进制的字典树,数组模拟)
    hdu 1800 Flying to the Mars(简单模拟,string,字符串)
    codeforces 425A Sereja and Swaps(模拟,vector,枚举区间)
    codeforces 425B Sereja and Table(状态压缩,也可以数组模拟)
    HDU 4148 Length of S(n)(字符串)
    codeforces 439D Devu and Partitioning of the Array(有深度的模拟)
    浅谈sass
    京东楼层案例思维逻辑分析
    浅谈localStorage和sessionStorage
  • 原文地址:https://www.cnblogs.com/shenxiaolin/p/5334223.html
Copyright © 2011-2022 走看看