Sunday, November 25, 2012

LINE CLIPPING USING COHEN SUTHERLAND ALGORITHM


LINE CLIPPING USING COHEN SUTHERLAND ALGORITHM

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#define FALSE 0
#define TRUE 1
#define LEFTEDGE 0x1
#define RIGHTEDGE 0x2
#define TOPEDGE 0x8
#define BOTTOMEDGE 0x4
#define INSIDE(a) (!a)
#define REJECT(a,b) (a&b)
#define ACCEPT(a,b) (!(a|b))

unsigned char encode(int x,int y,int minx,int maxx,int miny,int maxy)
{
            unsigned char code=0X00;
            if(x<minx)
                        code=code|LEFTEDGE;
            if(x>maxx)
                        code=code|RIGHTEDGE;
            if(y<miny)
                        code=code|BOTTOMEDGE;
            if(y>maxy)
                        code=code|TOPEDGE;
            return(code);
}
void swappts(int *x1,int *y1,int *x2,int *y2)
{
            int tx,ty;
            tx=*x1;
            ty=*y1;
            *x1=*x2;
            *y1=*y2;
            *x2=tx;
            *y2=ty;
}
void swapcode(unsigned char *c1,unsigned char *c2)
{
            unsigned char temp;
            temp=*c1;
*c1=*c2;
*c2=temp;
}



void clipline(int minx,int maxx,int miny,int maxy,int x1,int y1,int x2,int y2)
{
            unsigned char code1,code2;
            int done=FALSE;
            int draw=FALSE;
            float m;
            while(!done)
            {
                        code1=encode(x1,y1,minx,maxx,miny,maxy);
                        code2=encode(x2,y2,minx,maxx,miny,maxy);
                        if(ACCEPT(code1,code2))
                        {
                                    done=TRUE;
                                    draw=TRUE;
                        }
                        else if(REJECT(code1,code2))
                                    done=TRUE;
                        else
                        {
                                    if(INSIDE(code1))
                                    {
                                                swappts(&x1,&y1,&x2,&y2);
                                                swapcode(&code1,&code2);
                                    }
                                    if(x2!=x1)
                                                m=(y2-y1)/(x2-x1);
                                    if(code1&LEFTEDGE)
                                    {
                                                y1+=(minx-x1)*m;
                                                x1=minx;
                                    }
                                    else if(code1&RIGHTEDGE)
                                    {
                                                y1+=(maxx-x1)*m;
                                                x1=maxx;
                                    }
                                    else if(code1&BOTTOMEDGE)
                                    {
                                                if(x2!=x1)
                                                            x1+=(miny-y1)/m;
                                                y1=miny;
                                    }
                                    else if(code1&TOPEDGE)
                                    {
                                                if(x2!=x1)
                                                x1+=(maxy-y1)/m;
                                                y1=maxy;
                                    }
                        }
            }
            if(draw)
                        line(x1,y1,x2,y2);
}

void main()
{
            int x1,x2,y1,y2,minx,miny,maxx,maxy;
            int gd=DETECT,gm;
            initgraph(&gd,&gm,"");
            printf("Enter the minimum and maximum values of x:");
            scanf("%d%d",&minx,&maxx);
            printf("Enter the minimum and maximum values of y: ");
            scanf("%d%d",&miny,&maxy);
            printf("Enter the first enpoint of line: ");
            scanf("%d%d",&x1,&y1);
            printf("Enter the second endpoint of line: ");
            scanf("%d%d",&x2,&y2);
            printf("BEFORE CLIPPING");
            rectangle(minx,maxy,maxx,miny);
            line(x1,y1,x2,y2);
            getch();
            clrscr();
            printf("AFTER CLIPPING");
            rectangle(minx,maxy,maxx,miny);
            clipline(minx,maxx,miny,maxy,x1,y1,x2,y2);
            getch();
            closegraph();
}



















OUTPUT:-
           
            Enter the minimum and maximum values of x: 300  400

            Enter the minimum and maximum values of y: 300  400

            Enter the first endpoint  of line: 275  300

            Enter the second endpoint of line: 350  420

            BEFORE CLIPPING
           

                                                                               


 AFTER CLIPPING                            


                                                                                                                                     
                                                                       
                                                      

         


0 comments:

Post a Comment