TanZek's 技术空间

勇往直前,专注于技术...

首页 新随笔 联系 聚合 管理
  7 Posts :: 19 Stories :: 13 Comments :: 0 Trackbacks
DDA直线的生成算法:
[初级版]
 1 POINT A = { 100 , 100 } , B = { 200 , 200 } ;
 2 void  dda_line(HDC &  hdc)
 3 {
 4       double  length;
 5       double  dx,dy;
 6       double  x,y;
 7       char  a[ 20 ];
 8
 9       if (abs(B.x - A.x)  >=  abs(B.y - A.y))
10           length = abs(B.x - A.x);
11       else
12           length = abs(B.y - A.y);
13      
14      dx = ((B.x - A.x) / length);
15      dy = ((B.y - A.y) / length);
16
17      x = A.x + 0.5 ;
18      y = A.y + 0.5 ;
19      sprintf(a, " A(%d,%d) " , int (x), int (y));
20      TextOut(hdc, int (x), int (y),a, 10 );
21       int  i = 1 ;
22       while (i  <=  length)
23       {
24           SetPixel(hdc, int (x), int (y),RGB( 0 , 0 , 0 ));
25           x = x + dx;
26           y = y + dy;
27           i ++ ;
28      }

29     sprintf(a, " B(%d,%d) " , int (x), int (y));
30     TextOut(hdc, int (x), int (y),a, 10 );
31      return ;
32 }

33

看书上出来的一段DDA算法。实践出来了,拿上来记忆~~
[修改版]
 1 void  dda_line(HDC &  hdc, POINT A, POINT B,  int  color)
 2 {
 3       double  length;
 4       double  dx,dy;
 5       double  x,y;
 6       if (abs(B.x - A.x)  >=  abs(B.y - A.y))
 7           length = abs(B.x - A.x);
 8       else
 9           length = abs(B.y - A.y);
10     
11      dx = ((B.x - A.x) / length);
12      dy = ((B.y - A.y) / length);
13
14      x = A.x + 0.5 ;
15      y = A.y + 0.5 ;
16       int  i = 1 ;
17       while (i  <=  length)
18       {
19           SetPixel(hdc, int (x), int (y), color);
20           x = x + dx;
21           y = y + dy;
22           i ++ ;
23      }

24      return ;
25 }

26

为使算法更适用于各种编程方法,加入A和B的点参数,更加入颜色值color。在我的实际应用中,color被用来清除上一条画线结果的。
posted on 2006-10-07 06:42 TanZek 阅读(2274) 评论(0)  编辑 收藏 引用 所属分类: Subject-Study

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理