manthanthakker40

Cohen-Sutherland line clipping algorithm

Sep 23rd, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.70 KB | None | 0 0
  1. //---Cohen-Sutherland line clipping program-----
  2. #include<conio.h>
  3. #include<stdio.h>
  4. #include<graphics.h>
  5. #include<dos.h>
  6. #include<stdlib.h>
  7. typedef struct coordinate
  8. {
  9. int x,y;
  10. char code[10];
  11. }PT;
  12.  
  13.      //function to draw window
  14.      void drawwindow()
  15.      {
  16.     setcolor(RED);
  17.     line(150,100,450,100);
  18.     line(450,100,450,350);
  19.     line(450,350,150,350);
  20.     line(150,350,150,100);
  21.      }
  22.  
  23.      void drawline(PT p1,PT p2,int cl)
  24.      {
  25.     setcolor(cl);
  26.     line(p1.x,p1.y,p2.x,p2.y);
  27.      }
  28.      //function to set code
  29.      PT setcode(PT p)
  30.      {
  31.     PT ptemp;
  32.     if(p.y<100)
  33.     ptemp.code[0]='1';
  34.     else
  35.     ptemp.code[0]='0';
  36.     if(p.y>350)
  37.     ptemp.code[1]='1';
  38.     else
  39.     ptemp.code[1]='0';
  40.     if(p.x>450)
  41.     ptemp.code[2]='1';
  42.     else
  43.     ptemp.code[2]='0';
  44.     if(p.x<150)
  45.     ptemp.code[3]='1';
  46.     else
  47.     ptemp.code[3]='0';
  48.     ptemp.x=p.x;
  49.     ptemp.y=p.y;
  50.     return(ptemp);
  51.     }
  52.     //
  53.     int visibility(PT p1,PT p2)
  54.     {
  55.  
  56.         int i,flag=0;
  57.  
  58.         for(i=0;i<4;i++)
  59.         {
  60.         if((p1.code[i]!=0)||(p2.code[i]!=0))
  61.         flag=1;
  62.         }
  63.         if(flag==0)
  64.         return(0);
  65.         for(i=0;i<4;i++)
  66.         {
  67.             if((p1.code[i]==p2.code[i])&&(p1.code[i]=='1'))
  68.             flag=0;
  69.         }
  70.         if(flag==0)
  71.         return(1);
  72.         return(2);
  73.     }
  74.     PT resetendpt(PT p1,PT p2)
  75.     {
  76.  
  77.  
  78.         PT temp;
  79.         int x,y,i;
  80.         float m,k;
  81.  
  82.         if(p1.code[3]=='1')
  83.         x=150;
  84.         if(p1.code[2]=='1')
  85.         x=450;
  86.         if((p1.code[3]=='1')||(p1.code[2]=='1'))
  87.         {
  88.         m=(float)(p2.y-p1.y)/(p2.x-p1.x);
  89.         k=(p1.y+(m*(x-p1.x)));
  90.         temp.y=k;
  91.         temp.x=x;
  92.         for(i=0;i<4;i++)
  93.         temp.code[i]=p1.code[i];
  94.         if(temp.y<=350&&temp.y>=100)
  95.         return (temp);
  96.         }
  97.         if(p1.code[0]=='1')
  98.         y=100;
  99.         if(p1.code[1]=='1')
  100.         y=350;
  101.         if((p1.code[0]=='1')||(p1.code[1]=='1'))
  102.         {
  103.             m=(float)(p2.y-p1.y)/(p2.x-p1.x);
  104.         k=(float)p1.x+(float)(y-p1.y)/m;
  105.         temp.x=k;
  106.         temp.y=y;
  107.         for(i=0;i<4;i++)
  108.         temp.code[i]=p1.code[i];
  109.         return (temp);
  110.         }
  111.         else
  112.         return(p1);
  113.     }
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122. int main()
  123. {
  124.      int gd=DETECT,gm,v;
  125.      PT p1,p2,ptemp;
  126.      clrscr();
  127.      initgraph(&gd,&gm,"c:/tc1/tc/tc/bgi");
  128.      cleardevice();
  129.      printf("enter endpoint 1\n");
  130.      scanf("%d%d",&p1.x,&p1.y);
  131.      printf("enter endpoint 2\n");
  132.      scanf("%d%d",&p2.x,&p2.y);
  133.      getch();
  134.      cleardevice();
  135.      drawwindow();
  136.      getch();
  137.      drawline(p1,p2,15);
  138.      getch();
  139.      printf("worki");
  140.      p1=setcode(p1);
  141.      p2=setcode(p2);
  142.      v=visibility(p1,p2);
  143.      switch(v)
  144.      {
  145.     case 0: cleardevice();
  146.     drawwindow();
  147.     drawline(p1,p2,15);
  148.     break;
  149.     case 1:cleardevice();
  150.     drawwindow();
  151.     break;
  152.     case 2:
  153.  
  154.  
  155.     getch();
  156.       cleardevice();
  157.  
  158.        p1=resetendpt(p1,p2);
  159.     p2=resetendpt(p2,p1);
  160.         drawwindow();
  161.     drawline(p1,p2,15);
  162.     getch();
  163.     break;
  164.      }
  165.      getch();
  166.      closegraph();
  167.      return(0);
  168.  
  169.      }
Advertisement
Add Comment
Please, Sign In to add comment