| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2754 人关注过本帖, 1 人收藏
标题:[原创]TC实现DDA,中点画线算法,Bresenham算法
只看楼主 加入收藏
nainiu
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2004-6-6
收藏(1)
 问题点数:0 回复次数:1 
[原创]TC实现DDA,中点画线算法,Bresenham算法
大家都知道什么是DDA,中点划线,Brensenham算法吧?什么?不知道?那么我的这个帖子你可以跳过了~拜拜~~ 好了,闲话少叙,小弟我抛砖意图引玉,贴出程序,希望高手能指点一二~有什么观点大家交流一下阿

#include <stdio.h> #include <graphics.h> #include <math.h>

int max(int a,int b) { if(a>b) return a; else return b;

} int min(int a,int b) {

if(a<b) return a; else return b;

} void DDA(int xs,int ys,int xe,int ye,int color) {

float dx,dy,k,y,x; dx = xe - xs; dy = ye - ys; if(dx == 0) { y = min(ys,ye); x = xs; while(y < max(ys,ye)) {

putpixel((int)x,(int)y,color); y = y + 1; delay(10000); } } else { k = dy/dx; if(fabs(k)<=1) { x = min(xs,xe); if(x == xs) y = ys; else y = ye; for(;x < max(xe,xs);x++) { putpixel((int)x,(int)(y+0.5),color); y = y + k; delay(10000); } } else { y = min(ys,ye); if(y == ys) x = xs; else x = xe; for(;y<max(ys,ye);y++) { putpixel((int)(x+0.5),(int)y,color); x = x + 1/k; delay(10000); } }

} } void MidPointLine(int xs,int ys,int xe,int ye ,int color) {

float delta1,delta2,d,x,y; float k,a,b; a = ys - ye; b = xe - xs; if(b == 0) { y = min(ys,ye); x = xs; while(y<max(ys,ye)) { putpixel(x,y,color); y++; } } else if(a == 0) { x = min(xs,xe); y = ys; while(x<max(xs,xe)) { putpixel(x,y,color); x++; } } else { k = -a/b; if(k<1&&k>0) { x = min(xs,xe); if((int)x == xs) y = ys; else { y = ye; a = -a; b = -b; } delta1 = 2*a; delta2 = 2*(a + b); d = 2*a + b; putpixel(x,y,color); while(x < max(xs,xe)) { if(d<=0) { x++; y++; d = d+delta2; } else { x++; d=d+delta1; } putpixel(x,y,color); } } else if(k>=1) { y = min(ys,ye); if((int)y == ys) { x = xs; } else { x = xe; a = -a; b = -b; } delta1 = 2*b; delta2 = 2*(a + b); d = 2*b + a; putpixel(x,y,color); while(y < max(ys,ye)) { if(d>0) { x++; y++; d = d+delta2; } else { y++; d=d+delta1; } putpixel(x,y,color);

} } else if(k<=-1) {

y = min(ys,ye); if((int)y == ys) x = xs; else { x = xe; a = -a; b = -b; } delta1 = 2*b; delta2 = 2*(b - a); d = 2*b - a; putpixel(x,y,color); while(y < max(ys,ye)) { if(d<0) { y++; d = d+delta2; } else { y++; x--; d=d+delta1; } putpixel(x,y,color); } } else {

x = min(xs,xe); y = ys; if((int)x == xs) y = ys; else { y = ye; a = -a; b = -b; } delta1 = 2*a; delta2 = 2*(a - b); d = 2*a - b; putpixel(x,y,color); while(x < max(xs,xe)) { if(d<=0) { x++; d = d+delta1; } else { x++; y--; d=d+delta2; } putpixel(x,y,color); } } } }

int BresenhamLine (int xs,int ys,int xe, int ye,int color)

{ int x, y, i,state; float k, d,dx, dy; dx = xe-xs; dy = ye- ys; d = 0; if(dx == 0) { y = min(ys,ye); x = xs; for(i = 0;i < fabs(dy); i++) { putpixel(x,y,color); y++; } return 1; } if(dy == 0) { x = min(xs,xe); y = ys; while(x < max(xs,xe)) { putpixel(x,y,color); x++; } return 1; } k = dy/dx; if(k<=1&&k>0) { state = 1; } else if(k>1) { state = 2; } else if(k>=-1&&k<0) { state = 3; } else if(k<-1) { state = 4 ; } switch(state) { case 1: x=min(xs,xe); if(x == xs) y = ys; else y = ye; for (i=0; i<=fabs(dx); i++)

{

d=d+k;

if(d>=0.5) { y++; d =d - 1; } x=x+1; putpixel(x, y, color);

} break; case 2:

y = min(ys,ye); if(y == ys) x = xs; else x = xe; for(i = 0;i<fabs(dy);i++) { d = d + 1/k; if(d>=0.5) { x++; d = d -1; } y++; putpixel(x,y,color); } break; case 3: x = min(xs,xe); if(x == xs) y = ys; else y = ye; for(i=0;i<fabs(dx);i++) { d = d +k; if(d <= -0.5) { y--; d = d+1; } x++; putpixel(x,y,color);

} break; case 4:

y = min(ys,ye); if(y ==ys) { x = xs; } else x = xe; for(i = 0;i<fabs(dy);i++) { d = d + 1/k; y++; if(d<=-0.5) { x--; d = d+1; } putpixel(x,y,color); } break; } return 1; }

int main() { int driver = DETECT,graphmode,c; printf("choice:1 for DDA,2 for MinPointLine,3 for BresenhamLine\n"); scanf("%d",&c); initgraph(&driver,&graphmode,"c:\\TC"); setbkcolor(BLACK); if(c == 1) { DDA(320,240,400,80,GREEN) ;

DDA(320,240,480,160,GREEN) ;

DDA(320,240,480,320,GREEN) ;

DDA(320,240,400,400,GREEN) ;

DDA(320,240,240,400,GREEN) ;

DDA(320,240,160,320,GREEN) ;

DDA(320,240,160,160,GREEN) ;

DDA(320,240,240,80,GREEN) ;

DDA(160,240,480,240,GREEN) ;

DDA(320,80,320,400,GREEN) ;

} else if(c == 2) { MidPointLine(320,240,400,80,GREEN) ; MidPointLine(320,240,480,160,GREEN) ; MidPointLine(320,240,480,320,GREEN) ; MidPointLine(320,240,400,400,GREEN) ; MidPointLine(320,240,240,400,GREEN) ; MidPointLine(320,240,160,320,GREEN) ; MidPointLine(320,240,160,160,GREEN) ; MidPointLine(320,240,240,80,GREEN) ; MidPointLine(480,240,160,240,GREEN) ; MidPointLine(320,80,320,400,GREEN) ; } else if(c == 3) { BresenhamLine(320,240,400,80,GREEN) ; BresenhamLine(320,240,480,160,GREEN) ; BresenhamLine(320,240,480,320,GREEN) ; BresenhamLine(320,240,400,400,GREEN) ; BresenhamLine(320,240,240,400,GREEN) ; BresenhamLine(320,240,160,320,GREEN) ; BresenhamLine(320,240,160,160,GREEN) ; BresenhamLine(320,240,240,80,GREEN) ; BresenhamLine(480,240,160,240,GREEN) ; BresenhamLine(320,80,320,400,GREEN) ; }

getch(); closegraph(); }

[此贴子已经被作者于2005-9-26 4:42:04编辑过]

搜索更多相关主题的帖子: DDA 算法 Bresenham 点画 
2005-09-26 04:39
lwllklly
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-3-8
收藏
得分:0 
楼主厉害哇~~赞,不过有三个小错误需要修正

楼主厉害哇~~赞,不过有三个小错误需要修正
中点画线法局部有三个错误

把k<=-1中的

if(d<0)
{
y++;
d = d+delta2;
}
else
{
y++;
x--;
d=d+delta1;
}

改为

if(d>0)
{
y++;
d = d+delta1;
}
else
{
y++;
x--;
d=d+delta2;
}

2006-03-08 10:39
快速回复:[原创]TC实现DDA,中点画线算法,Bresenham算法
数据加载中...
 
   



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

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