Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <graphics.h>
- #include <math.h>
- //Change R to change the number of points of polynomial
- #define R 3
- #define C 3
- #define PI 3.142
- float c[R][C]={{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
- float temp[R][C]={{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
- void printM(float a[][C]){
- int i, j;
- for(i = 0; i < R; i++){
- for(j = 0; j < C; j++){
- printf("%.2f\t\t", a[i][j]);
- }
- printf("\n");
- }
- printf("\n\n");
- }
- void initializeM(float obj[][C]){
- /*int i, j;
- printf("Enter the points:-\n");
- for(i = 0; i < R; i++){
- for(j = 0; j < C; j++){
- if(j<C-1) scanf("%f", &obj[i][j]);
- else obj[i][C-1] = 1.00;
- }
- printf("\n");
- } */
- obj[0][0]=100;
- obj[0][1]=100;
- obj[0][2]=1;
- obj[1][0]=200;
- obj[1][1]=100;
- obj[1][2]=1;
- obj[2][0]=150;
- obj[2][1]=50;
- obj[2][2]=1;
- }
- void mul(float a[][C], float b[][C]){
- int i, j, k;
- float sum;
- for(i = 0; i < R; i++){
- for(j = 0; j < C; j++){
- sum = 0;
- for(k = 0; k < R; k++)
- sum += a[i][k]*b[k][j];
- temp[i][j] = sum;
- }
- }
- for(i=0; i < R; i++)
- for(j=0; j < C; j++)
- c[i][j] = temp[i][j];
- }
- void startGraphics(){
- int gd=DETECT, gm;
- initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
- }
- void showGraphically(float a[][C]){
- int i, j;
- for(i=0, j=1; i<R-1; i++, j++)
- line((int)a[i][0], (int)a[i][1], (int)a[j][0], (int)a[j][1]);
- line((int)a[0][0], (int)a[0][1], (int)a[R-1][0], (int)a[R-1][1]);
- }
- int main(){
- int choice, choiceref;
- float t;
- float obj[R][C];
- float trans[R][C] = {{1, 0, 0} , {0, 1, 0} , {0, 0, 1}};
- float scale[R][C] = {{0, 0, 0} , {0, 0, 0} , {0, 0, 1}};
- float rot[R][C] = {{0, 0, 0} , {0, 0, 0} , {0, 0, 1}};
- float shear[R][C] = {{1, 0, 0} , {0, 1, 0} , {0, 0, 1}};
- float reflect[R][C] = {{1, 0, 0} , {0, 1, 0} , {0, 0, 1}};
- clrscr();
- initializeM(obj);
- printM(c);
- while(1){
- printf("1.Tranlate 2.Scale 3.Rotate 4.Shear 5.Reflect 6.Exit: ");
- scanf("%d", &choice);
- switch(choice){
- case 1: printf("\nEnter tx and ty values.\n");
- scanf("%f%f", &trans[2][0], &trans[2][1]);
- printM(trans);
- mul(c, trans);
- break;
- case 2: printf("Enter sx and sy values.\n");
- scanf("%f%f", &scale[0][0], &scale[1][1]);
- printM(scale);
- mul(c, scale);
- break;
- case 3: printf("Enter theta's value:");
- scanf("%f", &t);
- rot[0][0]=cos(t*PI/180);
- rot[0][1]=sin(t*PI/180);
- rot[1][0]=-sin(t*PI/180);
- rot[1][1]=cos(t*PI/180);
- printM(rot);
- mul(c, rot);
- break;
- case 4: printf("Enter shx and shy values.\n");
- scanf("%f%f", &shear[1][0], &shear[0][1]);
- printM(shear);
- mul(c, shear);
- break;
- case 5: printf("Reflection about 1.X-axis 2.Y-axis 3.Origin 4.y=x 5.y=-x: ");
- scanf("%d", &choiceref);
- switch(choiceref){
- case 1: reflect[1][1] = -1; break;
- case 2: reflect[0][0] = -1; break;
- case 3: reflect[1][1] = -1; reflect[0][0] = -1; break;
- case 4: reflect[1][1] = 0; reflect[0][0] = 0; reflect[0][1] = 1; reflect[1][0] = 1; break;
- case 5: reflect[1][1] = 0; reflect[0][0] = 0; reflect[0][1] = -1; reflect[1][0] = -1; break;
- default: printf("Invalid option!");
- }
- printM(reflect);
- mul(c, reflect);
- reflect[0][0]=1;
- reflect[0][1]=0;
- reflect[0][2]=0;
- reflect[1][0]=0;
- reflect[1][1]=1;
- reflect[1][2]=0;
- reflect[2][0]=0;
- reflect[2][1]=0;
- reflect[2][2]=1;
- break;
- case 6: goto out;
- default: printf("Enter a valid input(1 to 6)\n");
- }
- printM(c);
- }
- out: mul(obj, c);
- printM(c);
- getch();
- startGraphics();
- showGraphically(obj);
- getch();
- showGraphically(c);
- getch();
- closegraph();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement