sidrs

CGR Ass 3 - CS Line Clipping

Aug 29th, 2024 (edited)
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <graphics.h>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. static int LEFT = 1, RIGHT = 2, BOTTOM = 4, TOP = 8, xmin, ymin, xmax, ymax;
  7.  
  8. int getCode(int x, int y){
  9.     int code = 0;
  10.     if (y > ymax) code |= TOP;
  11.     if (y < ymin) code |= BOTTOM;
  12.     if (x < xmin) code |= LEFT;
  13.     if (x > xmax) code |= RIGHT;
  14.     return code;
  15. }
  16.  
  17. int main() {
  18.     int gd = DETECT, gm;
  19.     //cout << "Enter Window's max and min values: " << endl;
  20.     //cin >> xmax >> ymax >> xmin >> ymin;
  21.     xmax = 350;
  22.     ymax = 350;
  23.     xmin = 150;
  24.     ymin = 150;
  25.  
  26.     int x1, y1, x2, y2;
  27.     cout << "Enter Endpoints of the line: " << endl;
  28.     cin >> x1 >> y1 >> x2 >> y2;
  29.  
  30.  
  31.     initgraph(&gd, &gm, nullptr);
  32.  
  33.     rectangle(xmin, ymin, xmax, ymax);
  34.     setcolor(BLUE);
  35.     line(x1, y1, x2, y2);
  36.  
  37.     int outcode1 = getCode(x1, y1);
  38.     int outcode2 = getCode(x2, y2);
  39.     int accept = 0;
  40.  
  41.     while (1) {
  42.         float m = (float)(y2 - y1)/(x2 - x1);
  43.         if (outcode1 == 0 && outcode2 == 0) {
  44.             accept = 1;
  45.             break;
  46.         } else if ((outcode1 & outcode2) != 0) {
  47.             break;
  48.         } else {
  49.             int x, y, temp;
  50.             if (outcode1 == 0) {
  51.                 temp = outcode2;
  52.             } else {
  53.                 temp = outcode1;
  54.             }
  55.            
  56.             if (temp & TOP) {
  57.                 x = x1 + (ymax - y1)/m;
  58.                 y = ymax;
  59.             } else if (temp & BOTTOM) {
  60.                 x = x1 + (ymin - y1)/m;
  61.                 y = ymin;
  62.             } else if (temp & LEFT) {
  63.                 x = xmin;
  64.                 y = y1 + m*(xmin - x1);
  65.             } else if (temp & RIGHT) {
  66.                 x = xmax;
  67.                 y = y1 + m*(xmax - x1);
  68.             }
  69.  
  70.             if (temp == outcode1) {
  71.                 x1 = x;
  72.                 y1 = y;
  73.                 outcode1 = getCode(x1, y1);
  74.             } else {
  75.                 x2 = x;
  76.                 y2 = y;
  77.                 outcode2 = getCode(x2, y2);
  78.             }
  79.         }
  80.     }
  81.  
  82.     cout << "After Clipping: ";
  83.     if (accept){
  84.         //cleardevice();
  85.    
  86.     }
  87.     setcolor(WHITE);
  88.     rectangle(xmin, ymin, xmax, ymax);
  89.     setcolor(RED);
  90.     line(x1, y1, x2, y2);
  91.     getch();
  92.     closegraph();
  93.  
  94.     return 0;
  95. }
  96.  
  97.  
  98. /*
  99. Sample Input:
  100. 100 400
  101. 400 200
  102. */
Advertisement
Add Comment
Please, Sign In to add comment