| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 785 人关注过本帖
标题:vc++画直线
只看楼主 加入收藏
xanglan
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2007-4-11
收藏
 问题点数:0 回复次数:0 
vc++画直线
void CAbcView::myBresenhamLine(int x1, int y1, int x2, int y2)
{
   CDC* myDDC;
   myDDC=GetDC();
   int x,y;
   float p,k,dx,dy;
   if(x1>x2||y1>y2)
   {
       x=x1;
       y=y1;
       x1=x2;
       y1=y2;
       x2=x;
       y2=y;
   }
   dx=(float)x2-x1;
   dy=(float)y2-y1;
   x=x1;
   y=y1;
   p=(float)2*dy-dx;
   myDDC->SetPixel(x1,y1,RGB(255,0,0));
   if(x1==x2&&y1==y2)
   {}
   else
   {
   if(x1==x2)
   {
       for(int i=y1+1;i<=y2;i++)
       {
           myDDC->SetPixel(x1,i,RGB(255,0,0));
       }
   }
   else
   {
       if(y1==y2)
       {
           for(int j=x1+1;j<=x2;j++)
           {
               myDDC->SetPixel(j,y1,RGB(255,0,0));
           }
       }
       else
       {
           if(dx!=0)
           {
               k=(float)dy/dx;
               if(k>0&&k<=1)
               {
                   for(int a=x1;a<=x2;a++)
                   {
                       if(p>0)
                       {
                           x=x+1;
                           y=y+1;
                           p=p+2*dy-2*dx;
                       }
                       else
                       {
                           x=x+1;
                           y=y;
                           p=p+2*dy;
                       }
                       myDDC->SetPixel(x,y,RGB(255,0,0));
                   }
               }
               else
               {
                   for(int b=y1;b<=y2;b++)
                   {
                       if(p>0)
                       {
                           x=x+1;
                           y=y+1;
                           p=p+2*dx-2*dy;
                       }
                       else
                       {
                           x=x;
                           y=y+1;
                           p=p+2*dx;
                       }
                       myDDC->SetPixel(x,y,RGB(255,0,0));
                   }
               }
           }
       }
   }
  }

}
搜索更多相关主题的帖子: 直线 
2007-12-27 19:58
快速回复:vc++画直线
数据加载中...
 
   



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

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