Advertisement
Shailrshah

Homogeneous Transformations

Oct 25th, 2014
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <graphics.h>
  4. #include <math.h>
  5.  
  6. //Change R to change the number of points of polynomial
  7. #define R 3
  8. #define C 3
  9. #define PI 3.142
  10.  
  11. float c[R][C]={{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
  12. float temp[R][C]={{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
  13.  
  14. void printM(float a[][C]){
  15.     int i, j;
  16.     for(i = 0; i < R; i++){
  17.         for(j = 0; j < C; j++){
  18.             printf("%.2f\t\t", a[i][j]);
  19.         }
  20.         printf("\n");
  21.     }
  22.     printf("\n\n");
  23. }
  24.  
  25. void initializeM(float obj[][C]){
  26.     /*int i, j;
  27.     printf("Enter the points:-\n");
  28.     for(i = 0; i < R; i++){
  29.         for(j = 0; j < C; j++){
  30.             if(j<C-1)  scanf("%f", &obj[i][j]);
  31.             else obj[i][C-1] = 1.00;
  32.         }
  33.         printf("\n");
  34.     } */
  35.     obj[0][0]=100;
  36.     obj[0][1]=100;
  37.     obj[0][2]=1;
  38.  
  39.     obj[1][0]=200;
  40.     obj[1][1]=100;
  41.     obj[1][2]=1;
  42.  
  43.     obj[2][0]=150;
  44.     obj[2][1]=50;
  45.     obj[2][2]=1;
  46. }
  47.  
  48. void mul(float a[][C], float b[][C]){
  49.     int i, j, k;
  50.     float sum;
  51.     for(i = 0; i < R; i++){
  52.         for(j = 0;  j < C; j++){
  53.             sum = 0;
  54.             for(k = 0; k < R; k++)
  55.                 sum += a[i][k]*b[k][j];
  56.             temp[i][j] = sum;
  57.         }
  58.     }
  59.     for(i=0; i < R; i++)
  60.         for(j=0; j < C; j++)
  61.             c[i][j] = temp[i][j];
  62. }
  63.  
  64. void startGraphics(){
  65.     int gd=DETECT, gm;
  66.     initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
  67. }
  68.  
  69. void showGraphically(float a[][C]){
  70.     int i, j;
  71.     for(i=0, j=1; i<R-1; i++, j++)
  72.         line((int)a[i][0], (int)a[i][1], (int)a[j][0], (int)a[j][1]);
  73.     line((int)a[0][0], (int)a[0][1], (int)a[R-1][0], (int)a[R-1][1]);
  74. }
  75.  
  76. int main(){
  77.     int choice, choiceref;
  78.     float t;
  79.     float obj[R][C];
  80.     float trans[R][C] = {{1, 0, 0} , {0, 1, 0} , {0, 0, 1}};
  81.     float scale[R][C] = {{0, 0, 0} , {0, 0, 0} , {0, 0, 1}};
  82.     float rot[R][C] = {{0, 0, 0} , {0, 0, 0} , {0, 0, 1}};
  83.     float shear[R][C] = {{1, 0, 0} , {0, 1, 0} , {0, 0, 1}};
  84.     float reflect[R][C] = {{1, 0, 0} , {0, 1, 0} , {0, 0, 1}};
  85.     clrscr();
  86.  
  87.     initializeM(obj);
  88.     printM(c);
  89.     while(1){
  90.         printf("1.Tranlate 2.Scale 3.Rotate 4.Shear 5.Reflect 6.Exit: ");
  91.         scanf("%d", &choice);
  92.         switch(choice){
  93.             case 1: printf("\nEnter tx and ty values.\n");
  94.                     scanf("%f%f", &trans[2][0], &trans[2][1]);
  95.                     printM(trans);
  96.                     mul(c, trans);
  97.                     break;
  98.             case 2: printf("Enter sx and sy values.\n");
  99.                     scanf("%f%f", &scale[0][0], &scale[1][1]);
  100.                     printM(scale);
  101.                     mul(c, scale);
  102.                     break;
  103.             case 3: printf("Enter theta's value:");
  104.                     scanf("%f", &t);
  105.                     rot[0][0]=cos(t*PI/180);
  106.                     rot[0][1]=sin(t*PI/180);
  107.                     rot[1][0]=-sin(t*PI/180);
  108.                     rot[1][1]=cos(t*PI/180);
  109.                     printM(rot);
  110.                     mul(c, rot);
  111.                     break;
  112.             case 4: printf("Enter shx and shy values.\n");
  113.                     scanf("%f%f", &shear[1][0], &shear[0][1]);
  114.                     printM(shear);
  115.                     mul(c, shear);
  116.                     break;
  117.             case 5: printf("Reflection about 1.X-axis 2.Y-axis 3.Origin 4.y=x 5.y=-x: ");
  118.                     scanf("%d", &choiceref);
  119.                     switch(choiceref){
  120.                         case 1: reflect[1][1] = -1; break;
  121.                         case 2: reflect[0][0] = -1; break;
  122.                         case 3: reflect[1][1] = -1; reflect[0][0] = -1; break;
  123.                         case 4: reflect[1][1] = 0; reflect[0][0] = 0; reflect[0][1] = 1; reflect[1][0] = 1; break;
  124.                         case 5: reflect[1][1] = 0; reflect[0][0] = 0; reflect[0][1] = -1; reflect[1][0] = -1; break;
  125.                         default: printf("Invalid option!");
  126.                     }
  127.                     printM(reflect);
  128.                     mul(c, reflect);
  129.                     reflect[0][0]=1;
  130.                     reflect[0][1]=0;
  131.                     reflect[0][2]=0;
  132.  
  133.                     reflect[1][0]=0;
  134.                     reflect[1][1]=1;
  135.                     reflect[1][2]=0;
  136.  
  137.                     reflect[2][0]=0;
  138.                     reflect[2][1]=0;
  139.                     reflect[2][2]=1;
  140.                     break;
  141.             case 6: goto out;
  142.             default: printf("Enter a valid input(1 to 6)\n");
  143.         }
  144.         printM(c);
  145.     }
  146.     out: mul(obj, c);
  147.     printM(c);
  148.     getch();
  149.     startGraphics();
  150.     showGraphically(obj);
  151.     getch();
  152.     showGraphically(c);
  153.     getch();
  154.     closegraph();
  155.     return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement