| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 412 人关注过本帖
标题:在MFC中用bresenha算法画直线,结果画出了折线。求高手指教!!
只看楼主 加入收藏
YY_WX
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-10-27
结帖率:0
收藏
已结贴  问题点数:10 回复次数:7 
在MFC中用bresenha算法画直线,结果画出了折线。求高手指教!!
int CcgdemoView::BresenhamLine(CDC* pDC, int x0,int y0,int x1, int y1,int color)
{
   //竖线(为避免k=inf)
    if(x0==x1)
    {
        if(y0<=y1)
            for(int y=y0;y<=y1;y++) pDC->SetPixel(x0,y,color);
        else
            for(int y=y0;y>=y1;y--) pDC->SetPixel(x0,y,color);
        return 0;
    }

    //横线(仅为提高效率)
    if(y0==y1)
    {
        if(x0<=x1)
            for(int x=x0;x<=x1;x++) pDC->SetPixel(x,y0,color);
        else
            for(int x=x0;x>=x1;x--) pDC->SetPixel(x,y0,color);
        return 0;
    }
    //其他情况   共分两大类:x0<x1和x0>x1。每一类下分四种情况:k<-1,  -1<k<0,  0<k<1, k>1,共八种情况。
    else
  {
    pDC->SetPixel(x0,y0,color);
    int x,y,dx,dy,temp,e;
    dx=x1-x0;dy=y1-y0;
    x=x0;y=y0;
    if(x0>x1)
    {
        if(dy>0)         //k<0                                               
        {   
            if(dy<abs(dx))    //-1<k<0                                    
            {  
                dx=-dx;dy=-dy;x=x1;y=y1;
                e=-dx;
                for(temp=0;temp<dx;temp++,x++)
                {
                    pDC->SetPixel(x,y,color);
                    e=e-2*dy;
                    //e=e+2*dy;
                    if(e>=0)
                    {
                        y--;
                        e=e-2*dx;
                    }
                }
             }
            else if(dy>abs(dx))      //   k<-1                                
            {   
                e=-dy;
                for(temp=0;temp<dy;temp++,y++)
                {
                    pDC->SetPixel(x,y,color);
                    e=e-2*dx;
                    //e=e+2*dx;
                    if(e>=0)
                    {
                        x--;
                        e=e-2*dy;
                    }
                 }
             }

          }
         else if(dy<0)          // k>0                                          
         {
            if(abs(dy)<abs(dx))       // 0<k<1
            {   
                dx=-dx;dy=-dy;x=x1;y=y1;
                e=-dx;
                for(temp=0;temp<dx;temp++,x++)
                {
                    pDC->SetPixel(x,y,color);
                    e=e+2*dy;
                    //e=e-2*dy;
                    if(e>=0)
                    {
                        y++;
                        e=e-2*dx;
                    }
                }
             }
            if(abs(dy)>abs(dx))                        //  k>1   
            {      
                dx=-dx;dy=-dy;x=x1;y=y1;
                e=-dy;
                for(temp=0;temp<abs(dy);temp++,y++)
                {
                    pDC->SetPixel(x,y,color);
                    e=e+2*dx;
                    //e=e-2*dx;
                    if(e>=0)
                    {
                        x++;
                        e=e-2*dy;
                    }
                }
             }
        }
    }
    if(x0<x1)
    {
        if(dy<0)                //k<0
        {  
            if(abs(dy)<dx)     //  -1<k<0        
            {
                e=-dx;
                for(temp=0;temp<dx;temp++,x++)
                {
                    pDC->SetPixel(x,y,color);
                    e=e-2*dy;
                    //e=e+2*dy;
                    if(e>=0)
                    {
                        y--;
                        e=e-2*dx;
                    }
                }
             }
             if(abs(dy)>dx)      // k<-1      
             {   
                 dx=-dx;dy=-dy;x=x1;y=y1;
                 e=-dy;
                 for(temp=0;temp<abs(dy);temp++,y++)
                 {
                     pDC->SetPixel(x,y,color);
                     e=e-2*dx;
                    // e=e+2*dx;
                     if(e>=0)
                     {
                         x--;
                         e=e-2*dy;
                     }
                   }
              }
           }
        if(dy>0)       //k>0  
        {
            if(dy>dx)     // k>1            
            {  
                e=-dy;
                for(temp=0;temp<dy;temp++,y++)
                {
                    pDC->SetPixel(x,y,color);
                    e=e+2*dx;
                    //e=e-2*dx;
                    if(e>=0)
                    {
                        x++;
                        e=e-2*dy;
                    }
                }
             }
             if(dy<dx)     //  0<k<1     
             {
                e=-dx;
                for(temp=0;temp<dx;temp++,x++)
                {
                    pDC->SetPixel(x,y,color);
                    e=e+2*dy;
                    //e=e-2*dy;
                    if(e>=0)
                    {
                        y++;
                        e=e-2*dx;
                    }
                 }
             }
         }
     }
    return 0;
 }

}
搜索更多相关主题的帖子: color return 
2012-10-27 11:31
YY_WX
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-10-27
收藏
得分:0 
跪求高手指教啊!!
2012-10-27 12:59
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
程序代码:
        if(x0<x1)
        {
            if(dy<0)                //k<0
            { 
                if(abs(dy)<dx)     //  -1<k<0       
                {
                    e=-dx;
                    for(temp=0;temp<dx;temp++,x++)
                    {
                        pDC->SetPixel(x,y,color);
                        e=e-2*dy;
                        //e=e+2*dy;
                        if(e>=0)
                        {
                            y--;
                            e=e-2*dx;
                        }
                    }
                }
                if(abs(dy)>dx)      // k<-1      
                {  
                    dx=-dx;dy=-dy;x=x1;y=y1;
                    e=-dy;
                    for(temp=0;temp<abs(dy) && y<=y0;temp++,y++)
                    {
                        pDC->SetPixel(x,y,color);
                        e=e-2*dx;
                        // e=e+2*dx;
                        if(e>=0)
                        {
                            x--;
                            e=e-2*dy;
                        }
                    }
                }
            }
            if(dy>0)       //k>0 
            {
                if(dy>dx)     // k>1           
                { 
                    e=-dy;
                    for(temp=0;temp<dy && y<=y0;temp++,y++)
                    {
                        pDC->SetPixel(x,y,color);
                        e=e+2*dx;
                        //e=e-2*dx;
                        if(e>=0)
                        {
                            x++;
                            e=e-2*dy;
                        }
                    }
                }
                if(dy<dx)     //  0<k<1    
                {
                    e=-dx;
                    for(temp=0;temp<dx && y<=y0;temp++,x++)
                    {
                        pDC->SetPixel(x,y,color);
                        e=e+2*dy;
                        //e=e-2*dy;
                        if(e>=0)
                        {
                            y++;
                            e=e-2*dx;
                        }
                    }
                }
            }
        }
        return 0;
    }
2012-10-27 15:07
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
回复 3楼 寒风中的细雨
操作的时候只发现了  会在y方向上有问题
2012-10-27 15:09
YY_WX
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-10-27
收藏
得分:0 
还是没有解决问题啊。我想知道我的程序错在哪里。斜率k>1时就出错,其他情况可以满足要求,我都给弄晕了。不过还是很感谢你哈。我再看看源代码。
2012-10-27 16:50
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
上传张图  看看  怎么让它出现   代码就没看了

会出现折线  是因为你画线的边界没有控制好

[ 本帖最后由 寒风中的细雨 于 2012-10-27 17:32 编辑 ]
2012-10-27 17:25
YY_WX
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-10-27
收藏
得分:0 
我弄好了,但是不知道为什么要这么做。。。
 if(dy>0)       //k>0  
         {
             if(dy>dx)     // k>1            
             {  
                 e=-dy;
                 for(temp=0;(temp<dy)&&(y<y1);temp++,y++)   //仅此处做修改,就能满足要求。但我不知道为什么要这么做。此处的y是从y0到y1的(y0<y1)y本身就比y1
                 {                                            小,加上(y<y1)这句不是有点多余么?
                     pDC->SetPixel(x,y,color);
                     e=e+2*dx;
                     //e=e-2*dx;
                     if(e>=0)
                     {
                         x++;
                         e=e-2*dy;
                     }
                 }
              }
2012-10-27 20:11
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:10 
回复 7楼 YY_WX
边界问题  理清楚了就行
2012-10-27 23:37
快速回复:在MFC中用bresenha算法画直线,结果画出了折线。求高手指教!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032942 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved