//Reference:- David F. Rogers "Procedural Elements for Computer Graphics" Second Edition #include<stdio.h> #include<graphics.h> #include<math.h> #define ROUND(a)((int)(a+0.5)) struct point { int x,y; }pol[10]; int n; void DDA(int,int,int,int); void Bresenham(int,int,int,int); void readpoly() { int i; for(i=0;i<n;i++) { printf("\nEnter the %d coordinates ",i+1); scanf("%d%d",&pol[i].x,&pol[i].y); } } void drawpolygonDDA() { int i; for(i=0;i<n;i++) DDA(pol[i].x,pol[i].y,pol[(i+1)%n].x,pol[(i+1)%n].y); } void drawpolygonBres() { int i; for(i=0;i<n;i++) { delay(500); Bresenham(pol[i].x,pol[i].y,pol[(i+1)%n].x,pol[(i+1)%n].y); } } int main() { int xa,ya,xb,yb,choice,gd=DETECT,gm=VGAMAX; printf("\nEnter the no of sides "); scanf("%d",&n); readpoly(); initgraph(&gd,&gm,NULL); printf("Press any key to draw traingle using Bresenham line drawing algortihm over the triangle drawn using DDA line drawing algorithm"); drawpolygonDDA(); getch(); drawpolygonBres(); while (!kbhit()); closegraph(); return 0; } void DDA(int xa,int ya,int xb,int yb) { int dx,dy,steps,k; dx=xb-xa; dy=yb-ya; float x=xa,y=ya,xinc,yinc; if(abs(dx)>abs(dy)) steps=abs(dx); else steps=abs(dy); xinc=dx/(float)steps; yinc=dy/(float)steps; putpixel(ROUND(x),ROUND(y),5); for(k=0;k<steps;k++) { x+=xinc; y+=yinc; putpixel(ROUND(x),ROUND(y),5); } } void Bresenham(int xa,int ya,int xb,int yb) { int i,dx=xb-xa,dy=yb-ya,p,twod,twodd,s; float m=2; if(dx!=0) m=(float)dy/(float)dx; if(m>=-1&&m<=1) { if(xa>xb) { Bresenham(xb,yb,xa,ya); return; } p=2*dx-dy; if(dy<0) { s=-1; dy=-dy; } else s=1; putpixel(xa,ya,10); for(i=0;i<dy;i++) { while(p>0) { xa++; p-=2*dy; putpixel(xa,ya,10); } ya+=s; p+=2*dx; putpixel(xa,ya,10); } } else { if(ya>yb) { Bresenham(xb,yb,xa,ya); return; } p=2*dy-dx; if(dx<0) { s=-1; dx=-dx; } else s=1; putpixel(xa,ya,10); for(i=0;i<dx;i++) { while(p>0) { ya++; p-=2*dx; putpixel(xa,ya,10); } xa+=s; p+=2*dy; putpixel(xa,ya,10); } } }
Sunday, November 21, 2010
Draw a polygon. Use Bresenham line drawing algorithm

Subscribe to:
Post Comments (Atom)
nice thought
ReplyDelete