0% found this document useful (0 votes)
23 views

2D Transformation

This document discusses 2D transformation of geometric shapes using matrix multiplication. It defines functions for performing common 2D transformations like translation, rotation, scaling and reflection. These functions operate on an initial matrix representing a triangle and multiply it with transformation matrices to apply the transformations. The program demonstrates applying various sequences of transformations like reflection and rotation to dynamically transform the initial triangle.

Uploaded by

Jaiwick Soni
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

2D Transformation

This document discusses 2D transformation of geometric shapes using matrix multiplication. It defines functions for performing common 2D transformations like translation, rotation, scaling and reflection. These functions operate on an initial matrix representing a triangle and multiply it with transformation matrices to apply the transformations. The program demonstrates applying various sequences of transformations like reflection and rotation to dynamically transform the initial triangle.

Uploaded by

Jaiwick Soni
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical-2D transformation

#include<conio.h> #include<graphics.h> #include<math.h> #include<stdio.h> #define PI 30141592654 Typedef float matrix[3][3]; Matrix initial matrix; Enum axis {xaxis, yaxis, yequals_minusx, yequals_x}; Void unit_matrix(matrix m) { Int r, c; Matrix temp; For (r=0; r<3; r++) For (c=0; c<3; c++) M[r][c] = (r==c); } Void matrixmultiply(matrix a, matrixb) { Int r,c; Matrix temp; For (r=0; r<3; r++) For (c=0; c<3; c++) Temp[r][c] = a[r][0]*b[0][c] + a[r][1]*b[1]1c] + a[r][2]*b[2][c]; For (r=0; r<3; r++) For (c=0; c<3; c++) B[r][c] = temp[r][c];

} Void mirror2d(axis a) { Matrix m; Unit_matrix(m); Switch (a) { Case xaxis: M[1][1]= -1; Break; Case yaxis: M[0][0]= -1; Break; Case yequals_minusx: M[0]0[0] =0; m[0]1] = -1; m[1][0] = -1; m[1][1] = 0; Break; Case yequals_x: M[0]0[0] =0; m[0]1] = 1; m[1][0] = 1; m[1][1] = 0; Break; } Matrixmultiply(m, initialmatrix); } Void translate2d(float tx, float ty) { Matrix m; Unit_matrix(m);

M[0][2]= tx; m[1][2] =ty; Matrixmultiply(m,initialmatrix); } Void scale2d(float sx, float sy) { Matrix m; Unit_matrix(m); M[0][0] =sx; M[1][1] =sy; Matrixmultiply(m, initialmatrix); } Viod rotate2d (float radianagle) { Matrix m; Unit_matrix(m); M[0][0] = cos(radianangle); M[1][0] = sin(radianangle); M[0][1] = - sin(radianangle); M[1][1] = cos(radianangle); Matrixmultiply(m, initialmatrix); } Void main (void) { Int gd=DETECT(&gd,&gm,); Float rotateangle=270* PI/180; //defaining initialmatrix,a triangle Initialmatrix[0][0]=8;initialmatrix [0][1]=10; initialmatrix [0][2]=8;

initialmatrix [1][0]=2; initialmatrix [1][1]=4; initialmatrix [1][2]=6; initialmatrix [2][0]=1; initialmatrix [2][1]=1; initialmatrix [2][2]=1; setcolour (RED); //drawing initialmatrix (triangle) Line(initialmatrix[0][0], initialmatrix [1][0];, initialmatrix [0][1];, initialmatrix [1][1];); Line(initialmatrix [0][1], initialmatrix [1][1],initialmatrix [0][2], initialmatrix [1][2); Line(initialmatrix [0][1], initialmatrix [1][1],initialmatrix [0][2], initialmatrix [1][2);

Line((initial matrix [0 ][2],initial matrix [1][2],initial matrix [0][0],initial matrix [1][0] //wait for a keystroke get ch( ); //2D Operations //Refer Solved Example 4.11 //Mirroring about X axis and then about y = -x Mirror2d(xaxis); Mirror2d(yequals_minusx); //Print the final Matrix Printf(After applying reflection about x axis and y = - x\n); For (int i=0; i<3 ; i++) Printf(\t|%f\t%f\t%f|\n, initialmatrix[0][i], initialmatrix[1][i], initialmatrix[2][i]);s //Starting again with initial matrix initial matrix[0][0] = 8 ; initial matrix[0][1] = 10 ; initial matrix[0][2] = 8 ; initial matrix[1][0] = 2 ; initial matrix[1][1] = 4 ; initial matrix[1][2] = 6 ; initial matrix[2][0] = 8 ; initial matrix[2][1] = 1 ; initial matrix[2][2] = 1 ; // Rotating the triangle by 270 degrees Rotated2d (rotateangle); Printf(\nafter applying rotation of 270 degrees\n):

For (i=0;i<3;i++) Printf(/t|%f?t%f\t%f|n,initialmatrix[0][1],initialmatrix[1][i], Initialmatrix[2][i]; //translating and magnifying for proper visualization //of transformed triangle and displaying the transformed initialmatrix Scale2d(15,15); translated 2d(getmaxx()/2, getmaxy()/2 + 100); setcolor(GREEN); line(initialmatrix[0][0],initalmatrix[1][0],initialmatrix[0][1],initialmatrix[1][1]); line(initialmatrix[0][1],initalmatrix[1][1],initialmatrix[0][2],initialmatrix[1][2]); line(initialmatrix[0][2],initalmatrix[1][2],initialmatrix[0][0],initialmatrix[1][0]); getch(); }

You might also like