Posts

Showing posts from April, 2011

polygon program for graphics

#include #include #include #include #include void line(int xa,int ya,int xb,int yb) { int dx,dy,s,k; float xi,yi,x,y; int gd=DETECT,gm; initgraph(&gd,&gm, ""); dx=xb-xa; dy=yb-ya; if(abs(dx)>abs(dy)) s=abs(dx); else s=abs(dy); xi=dx/s; yi=dy/s; x=xa; y=ya; putpixel(ceil(x),ceil(y),1); for(k=0;k { x=x+xi; y=y+yi; putpixel(ceil(x),ceil(y),1); } } void main() { int v[10][2]; int x,y,n,i,x1,y1,x2,y2; printf("ENTER THE NO OF EDGES"); scanf("%d",&n); printf("ENTER THE VERTICES"); for(i=0;i { scanf("%d",&v[i][0]); scanf("%d",&v[i][1]); } for(i=0;i { if(i!=n-1) { x1=v[i][0]; y1=v[i][1]; x2=v[i+1][0]; y2=v[i+1][1]; } else { x1=v[i][0]; y1=v[i][1]; x2=v[0][0]; y2=v[0][1]; } line(x1,y1,x2,y2); } getch(); }

line clipping program

#include #include #include #include struct wind { int xmin,ymin; int xmax,ymax; }; struct point { int x,y; char code[5]; }; void gencode(struct wind w,struct point p) { if(p.x p.code[0]='1'; else p.code[0]='0'; if(p.x>w.xmax) p.code[1]='1'; else p.code[1]='0'; if(p.y p.code[2]='1'; else p.code[2]='0'; if(p.y>w.ymax) p.code[3]='1'; else p.code[3]='0'; } struct point clipline(struct wind w,struct point p,struct point pt) { int i,flag=0; float m=(p.y-pt.y)/(p.x-pt.x); gencode(w,p); while(!flag) { flag=1; if(p.code[0] == '1') { p.y+=(w.xmin-p.x)*m; p.x=w.xmin; gencode(w,p); flag=0; } else if(p.code[1] == '1') { p.y+=(w.xmax-p.x)*m; p.x=w.xmax; gencode(w,p); flag=0; } else if(p.code[2] == '1') { p.x+=(w.ymin-p.y)/m; p.y=w.ymin; gencode(w,p); flag=0; } else if(p.code[3] == '1') { p.x+=(w.ymax-p.y)...