#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编辑过]