M
MB
Hello,
I am trying to rotate the following matrix by 90 degrees.
1 2 3
4 5 6
7 8 9
By multiplying each co-ordinate by the matrix:
cos(90) sin(90)
-sin(90) cos(90)
I am using the following code:
#include <math.h>
int main() {
// Declare variables
int x,y,new_x, new_y;
// Declare matrices
int new_matrix[4][4]; // Working with first index at 1 not 0
int old_matrix[4][4];
// Populate matrix
old_matrix[1][1] = 1;
old_matrix[2][1] = 2;
old_matrix[3][1] = 3;
old_matrix[1][2] = 4;
old_matrix[2][2] = 5;
old_matrix[3][2] = 6;
old_matrix[1][3] = 7;
old_matrix[2][3] = 8;
old_matrix[3][3] = 9;
// Loop thorugh matrix
for (y=1; y<4; y++) {
for (x=1; x<4; x++) {
// Rotate co-ordinates by mutlipling co-ords by rotation matrix
new_x = ((x*cos(90)) - (y*sin(90)));
new_y = ((x*sin(90)) + (y*cos(90)));
// Copy pre-rotated values to new matrix using new co-ords
new_matrix[x][y] = old_matrix[new_x][new_y];
// Print out the new co-ordinates
printf("(%d,%d) ", new_x, new_y);
}
printf("\n");
}
return 0;
}
The output is coming out as:
(-1,0) (-1,1) (-2,2)
(-2,0) (-2,0) (-3,1)
(-3,0) (-3,0) (-4,1)
Can anyone see where I am going wrong?
Thank you,
Martin
I am trying to rotate the following matrix by 90 degrees.
1 2 3
4 5 6
7 8 9
By multiplying each co-ordinate by the matrix:
cos(90) sin(90)
-sin(90) cos(90)
I am using the following code:
#include <math.h>
int main() {
// Declare variables
int x,y,new_x, new_y;
// Declare matrices
int new_matrix[4][4]; // Working with first index at 1 not 0
int old_matrix[4][4];
// Populate matrix
old_matrix[1][1] = 1;
old_matrix[2][1] = 2;
old_matrix[3][1] = 3;
old_matrix[1][2] = 4;
old_matrix[2][2] = 5;
old_matrix[3][2] = 6;
old_matrix[1][3] = 7;
old_matrix[2][3] = 8;
old_matrix[3][3] = 9;
// Loop thorugh matrix
for (y=1; y<4; y++) {
for (x=1; x<4; x++) {
// Rotate co-ordinates by mutlipling co-ords by rotation matrix
new_x = ((x*cos(90)) - (y*sin(90)));
new_y = ((x*sin(90)) + (y*cos(90)));
// Copy pre-rotated values to new matrix using new co-ords
new_matrix[x][y] = old_matrix[new_x][new_y];
// Print out the new co-ordinates
printf("(%d,%d) ", new_x, new_y);
}
printf("\n");
}
return 0;
}
The output is coming out as:
(-1,0) (-1,1) (-2,2)
(-2,0) (-2,0) (-3,1)
(-3,0) (-3,0) (-4,1)
Can anyone see where I am going wrong?
Thank you,
Martin