sidrs

CG Ass 4 - Transformation

Sep 19th, 2024 (edited)
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <graphics.h>
  3. #include <cmath>
  4. #include <string>
  5. using namespace std;
  6.  
  7. class Transform {
  8. public:
  9.     int m, a[20][20], c[20][20];
  10.     int i, j, k;
  11. public:
  12.     void object();
  13.     void accept();
  14.    
  15.     void operator * (float b[20][20]){
  16.         for (i = 0; i < m; i++){
  17.             for (j = 0; j < m; j++) {
  18.                 c[i][j] = 0;
  19.                 for (k = 0; k < m; k++) {
  20.                     c[i][j] += (a[i][k] * b[k][j]);
  21.                 }
  22.             }
  23.         }
  24.     }
  25. };
  26.  
  27. void Transform::object(){
  28.     int gd, gm;
  29.     gd = DETECT;
  30.     initgraph(&gd, &gm, nullptr);
  31.     line(300, 0, 300, 600);
  32.     line(0, 300, 600, 300);
  33.  
  34.     for (i = 0; i < m - 1; i++){
  35.         line (300 + a[i][0], 300 - a[i][1], 300 + a[i + 1][0], 300 - a[i + 1][1]);
  36.     }
  37.     line (300 + a[0][0], 300 - a[0][1], 300 + a[i][0], 300 - a[i][1]);
  38.  
  39.     for (i = 0; i < m - 1; i++){
  40.         line (300 + c[i][0], 300 - c[i][1], 300 + c[i + 1][0], 300 - c[i + 1][1]);
  41.     }
  42.     line (300 + c[0][0], 300 - c[0][1], 300 + c[i][0], 300 - c[i][1]);
  43.  
  44.     int temp;
  45.     cout << "Press 1 to continue: " << endl;
  46.     cin >> temp;
  47.     closegraph();
  48. }
  49.  
  50. void Transform::accept() {
  51.     cout << "\n";
  52.     cout << "Enter the number of edges: ";
  53.     cin >> m;
  54.     cout << "\nEnter the Coordinates: ";
  55.     for (i = 0; i < m; i++){
  56.         for (j = 0; j < 3; j++){
  57.             if (j >= 2) a[i][j] = 1;
  58.             else cin >> a[i][j];
  59.         }
  60.     }
  61. }
  62.  
  63. int main() {
  64.     int ch, tx, ty, sx, sy;
  65.     float deg, theta, b[20][20];
  66.     Transform t;
  67.     t.accept();
  68.  
  69.     cout << "----- MENU -----" << endl;
  70.     cout << "1. Translation\n2. Scaling\n3. Rotation" << endl;
  71.     cout << "Enter your Choice: "; cin >> ch;
  72.     switch (ch) {
  73.         case 1:
  74.             cout << "Translation Operation" << endl;
  75.             cout << "Enter values for tx and ty: "; cin >> tx >> ty;
  76.             b[0][0] = b[2][2] = b[1][1] = 1;
  77.             b[0][1] = b[0][2] = b[1][0] = b[1][2] = 0;
  78.             b[2][0] = tx;
  79.             b[2][1] = ty;
  80.             t * b;
  81.             t.object();
  82.             break;
  83.  
  84.         case 2:
  85.             cout << "Scaling Operation" << endl;
  86.             cout << "Enter value for sx and sy: "; cin >> sx >> sy;
  87.             b[0][0] = sx;
  88.             b[1][1] = sy;
  89.             b[0][1] = b[0][2] = b[1][0] = b[1][2] = 0;
  90.             b[2][0]= b[2][1] = 0;
  91.             b[2][2] = 1;
  92.             t * b;
  93.             t.object();
  94.             break;
  95.  
  96.         case 3:
  97.             cout << "Rotation Operation" << endl;
  98.             cout << "Enter value for the angle: "; cin >> deg;
  99.             theta = deg * (3.14/100);
  100.             b[0][0] = b[1][1] = cos(theta);
  101.             b[0][1] = sin(theta);
  102.             b[1][0] = sin(-theta);
  103.             b[0][2] = b[1][2] = b[2][0] = b[2][1] = 0;
  104.             b[2][2] = 1;
  105.             t * b;
  106.             t.object();
  107.             break;
  108.  
  109.         default:
  110.             cout << "ERROR! Please Enter a valid choice " << endl;
  111.     }
  112.  
  113.     getch();
  114.  
  115.     return 0;
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment