Advertisement
Riz1Ahmed

Cohen Sutherland Line Clipping Algorithm

Sep 11th, 2020
1,481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<graphics.h>
  3. float abs (float n){ return n>0 ? n:-n;}
  4. float max(float x,float y){return x>y ? x:y;}
  5. float min(float x,float y){return x<y ? x:y;}
  6.  
  7. int winX1,winY1,winX2,winY2;
  8. void DrawWindow(){
  9.     for (int i=winX1; i<=winX2; i++)
  10.         putpixel(i, winY1, YELLOW), putpixel(i, winY2, YELLOW), delay(10);
  11.     for (int i=winY1; i<=winY2; i++)
  12.         putpixel(winX1, i, YELLOW), putpixel(winX2, i, YELLOW), delay(10);
  13. }
  14.  
  15. void DrawLineUsingDDA(float X0, float Y0, float X1, float Y1,int color){
  16.     float dx=X1-X0, dy=Y1-Y0;
  17.     float steps = max(abs(dx),abs(dy));
  18.     //Increment of x & y
  19.     float Xinc = dx / (float)steps;
  20.     float Yinc = dy / (float)steps;
  21.  
  22.     float X = X0, Y = Y0;
  23.     for (float i = 0; i <= steps; i++){
  24.         putpixel (X,Y,color);
  25.         X+=Xinc, Y+=Yinc;
  26.         delay(10);
  27.     }
  28. }
  29. int FindBits(int x,int y){
  30.     int bt=0;
  31.     if (y-max(winY1,winY2) > 0) bt|=(1<<3);
  32.     if (min(winY1,winY2)-y > 0) bt|=(1<<2);
  33.     if (x-max(winX1,winX2) > 0) bt|=(1<<1);
  34.     if (min(winX1,winX2)-x > 0) bt|=(1<<0);
  35.     return bt;
  36. }
  37. void CandidateTheLine(int initBits,float x1,float y1, int termBits,float x2,float y2){
  38.     float m=(y2-y1)/(x2-x1);
  39.     float cx1=x1,cy1=y1,cx2=x2,cy2=y2;
  40.     //printf("m=%.2f\n",m);
  41.     if (initBits&(1<<3)) y1=max(winY1,winY2);
  42.     if (initBits&(1<<2)) y1=min(winY1,winY2);
  43.     if (y1==max(winY1,winY2) || y1==min(winY1,winY2))
  44.         x1=cx1 + (1/m)*(y1-cy1);
  45.  
  46.     if (initBits&(1<<1)) x1=max(winX1,winX2);
  47.     if (initBits&(1<<0)) x1=min(winX1,winX2);
  48.     if (x1==max(winX1,winX2) || x1==min(winX1,winX2))
  49.         y1=cy1 + m*(x1-cx1);
  50.  
  51.     if (termBits&(1<<3)) y2=max(winY1,winY2);
  52.     if (termBits&(1<<2)) y2=min(winY1,winY2);
  53.     if (y2==max(winY1,winY2) || y2==min(winY1,winY2))
  54.         x2=cx1 + (1/m)*(y2-cy1);
  55.  
  56.     if (termBits&(1<<1)) x2=max(winX1,winX2);
  57.     if (termBits&(1<<0)) x2=min(winX1,winX2);
  58.     if (x2==max(winX1,winX2) || x2==min(winX1,winX2))
  59.         y2=cy1 + m*(x2-cx1);
  60.  
  61.     DrawLineUsingDDA(x1,y1,x2,y2,YELLOW);
  62. }
  63. int main(){
  64.     int bt=0;
  65.     bt|=(1<<0);
  66.     printf("%d\n",bt);
  67.     int gd = DETECT, gm;
  68.     initgraph (&gd, &gm, "");//Initialize graphics function
  69.     puts("***Mid Point Cirle Algorithm***\n");
  70.  
  71.     puts("Input the lower bound of window (winX1 winY1):");
  72.     scanf("%d %d", &winX1, &winY1);
  73.     puts("Input the upper bound of window (winX2 winY2):");
  74.     scanf("%d %d", &winX2, &winY2);
  75.     DrawWindow();
  76.  
  77.     int lx1,ly1,lx2,ly2;
  78.     while(1){
  79.         puts("Input the initial position of line (lx1 ly1):");
  80.         scanf("%d %d", &lx1, &ly1);
  81.         puts("Input the terminal position of line (lx2 ly2):");
  82.         scanf("%d %d", &lx2, &ly2);
  83.         DrawLineUsingDDA(lx1,ly1,lx2,ly2, YELLOW);
  84.         int initBits=FindBits(lx1,ly1);
  85.         int termBits=FindBits(lx2,ly2);
  86.  
  87.         if ((initBits & termBits)!=0){
  88.             puts("This line is out size of Region.");
  89.             delay(1000);
  90.             DrawLineUsingDDA(lx1,ly1,lx2,ly2, BLACK);
  91.             puts("So I removed it.");
  92.         }else{
  93.             puts("This line maybe candidate.");
  94.             DrawLineUsingDDA(lx1,ly1,lx2,ly2, BLACK);//Remove line
  95.             CandidateTheLine(initBits,lx1,ly1, termBits,lx2,ly2);
  96.         }
  97.         puts("\nTry another line.");
  98.     }
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement