Graphics programming using c
Home
Question Paperz
Java Projectz
ASP Projectz
8086
Data structures
c++
8085
Sunday, August 15, 2010
cohen-sutherland Line clipping
#include<stdio.h> #include<graphics.h> #define left 1 #define right 2 #define top 8 #define bottom 4 int xa,ya,xb,yb; int wxmax,wxmin,wymax,wymin; int regioncode(int x,int y) { int ret=0; if(x<wxmin) ret|=left; if(x>wxmax) ret|=right; if(y<wymin) ret|=bottom; if(y>wymax) ret|=top; return ret; } int clipline() { int rcode1=regioncode(xa,ya); int rcode2=regioncode(xb,yb); int x=0,y=0; float m=0.0; if(xb!=xa) m=(float)(yb-ya)/(float)(xb-xa); while(1) { if(rcode1==0&&rcode2==0) return 1; else if((rcode1&rcode2)!=0) return 0; else { int outcode=(rcode1==0)?rcode2:rcode1; if(outcode&top) { if(m!=0) x=xa+(float)(1/m)*(float)(wymax-ya); else x=xa; y=wymax; } else if(outcode&bottom) { if(m!=0) x=xa+(float)(1/m)*(float)(wymin-ya); else x=xa; y=wymin; } else if(outcode&left) { x=wxmin; y=ya+(float)(m)*(float)(wxmin-xa); } else if(outcode&right) { x=wxmax; y=ya+(float)(m)*(float)(wxmax-xa); } if(rcode1==outcode) { xa=x; ya=y; rcode1=regioncode(xa,ya); } else { xb=x; yb=y; rcode2=regioncode(xb,yb); } } } } int main() { int gd=DETECT,gm=VGAMAX; printf("\nEnter the diagonal of rectangle, wxmin,wymin,wxmax,wymax"); scanf("%d%d%d%d",&wxmin,&wymin,&wxmax,&wymax); printf("\nxmin ymin (%d,%d)\n xmax ymax (%d,%d)",wxmin,wymin,wxmax,wymax); printf("\nEnter the cordinates of line "); scanf("%d%d%d%d",&xa,&ya,&xb,&yb); initgraph(&gd,&gm,NULL); printf("Before Clipping "); rectangle(wxmin,wymin,wxmax,wymax); line(xa,ya,xb,yb); while(!kbhit()); closegraph(); initgraph(&gd,&gm,NULL); printf("After Clipping "); rectangle(wxmin,wymin,wxmax,wymax); if(clipline()) line(xa,ya,xb,yb); while(!kbhit()); closegraph(); return 0; }
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment