sidrs

CGR Ass 2

Aug 8th, 2024 (edited)
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <graphics.h>
  4. #include <cmath>
  5. using namespace std;
  6.  
  7. class ScanLine {
  8. public:
  9.     int x[20], y[20], k;
  10.     float slope[20], x_int[20];
  11.    
  12.     void polygon(int n){
  13.         int i;
  14.         float dx, dy;
  15.  
  16.         // Make First and Last Points of the Polygon the same
  17.         x[n] = x[0];
  18.         y[n] = y[0];
  19.        
  20.         // Draw Lines
  21.         for (int i = 0; i < n; i++){
  22.             line(x[i], y[i], x[i + 1], y[i + 1]);
  23.         }
  24.  
  25.         // Get Slopes of each Line
  26.         for (int i = 0; i < n; i++){
  27.             dy = y[i + 1] - y[i];
  28.             dx = x[i + 1] - x[i];
  29.             if (dy == 0) slope[i] = 1;
  30.             else if (dx == 0) slope[i] = 0;
  31.             else slope[i] = dx/dy;
  32.         }
  33.  
  34.         // Find Intersection Points
  35.         for (int p = 0; p < 512; p++) {
  36.             k = 0;
  37.             for (int i = 0; i < n; i++){
  38.                 if ( ((y[i] <= p) && (y[i + 1] > p)) || ((y[i] > p) && (y[i + 1] <= p) )) {
  39.                     x_int[k] = x[i] + slope[i] * (p - y[i]);
  40.                     k++;
  41.                 }
  42.             }
  43.  
  44.             // Sort (WHy THIS NOT WORKY!! :(( )
  45.             // int ok = sizeof(x_int)/sizeof(x_int[0]);
  46.             // sort(x_int, x_int+ok);
  47.  
  48.             // Bubble sort
  49.             for (int j = 0; j < k-1; j++){
  50.                 for (int i = 0; i < k-1; i++){
  51.                     if (x_int[i] > x_int[i+1]){
  52.                         int temp = x_int[i];
  53.                         x_int[i] = x_int[i+1];
  54.                         x_int[i+1] = temp;
  55.                     }
  56.                 }
  57.             }
  58.  
  59.             for (int i = 0; i < k; i+=2){
  60.                 setcolor(YELLOW);
  61.                 line(x_int[i], p, x_int[i+1], p);
  62.                 delay(30);
  63.  
  64.             }
  65.         }
  66.  
  67.     }
  68.    
  69. };
  70.  
  71. int main(){
  72.  
  73.    
  74.     int n, i;
  75.     ScanLine P;
  76.     cout << "Enter the number of Edges: "; cin >> n;
  77.     cout << "Enter the co-ordinates: " << endl;
  78.     for (int i = 0; i < n; i++){
  79.         cin >> P.x[i] >> P.y[i];
  80.     }
  81.  
  82.     int gd = DETECT; int gm;
  83.     initgraph(&gd, &gm, nullptr);
  84.  
  85.     P.polygon(n);
  86.    
  87.     getch();
  88.     closegraph();
  89.  
  90.     return 0;
  91. }
  92.  
  93.  
  94. /*
  95.  
  96. Enter the number of Edges: 3
  97. Enter the co-ordinates:
  98. 300 50
  99. 500 250
  100. 100 400
  101.  
  102. */
Advertisement
Add Comment
Please, Sign In to add comment