D.V.D
Make a wish
- Reaction score
- 73
So I'm trying to rotate a box/rectangle around a point so that the boxes rotation constantly changes but it's location stays the same. I have my values in the rotation matrix based of a few links but when I draw the rectangle/box, it's position is changed more and more as the rotation angle increases/decreases.
rotation matrix = [ cos theta , -sin theta
sin theta , cos theta ]
Here's the output I get in my window ( I drew all of the different rotations to see their positions clearly, and the bottom is erased in paint, not in the program ):
Note: I use a lot of constants in my code and normal arrays instead of dynamic arrays like I probably should for matrices. Also, this is my first time ever using them
rotation matrix = [ cos theta , -sin theta
sin theta , cos theta ]
Here's the output I get in my window ( I drew all of the different rotations to see their positions clearly, and the bottom is erased in paint, not in the program ):
Note: I use a lot of constants in my code and normal arrays instead of dynamic arrays like I probably should for matrices. Also, this is my first time ever using them
Code:
class position {
public:
float x,y;
};
void matrix_test (HDC hdc) {
// set initial position
int init_x = 700;
int init_y = 700;
// set matrices dimensions
const int mat_x = 5;
const int mat_y = 20;
// load matrices
float rotation [2][2];
position pos [mat_x][mat_y];
position display [mat_x][mat_y];
// load colors
COLORREF draw_col = RGB(0,0,0);
COLORREF delete_col = RGB(255,255,255);
// set the position of the box
for ( int i=0; i < mat_x; i++ ) {
for ( int j=0; j < mat_y; j++ ) {
pos[i][j].x=init_x+i;
pos[i][j].y=init_y+j;
}
}
for ( int i=0; i < mat_x; i++ ) {
for ( int j=0; j < mat_y; j++ ) {
SetPixel(hdc,pos[i][j].x,pos[i][j].y,draw_col);
}
}
//Sleep(1000);
// copy pos matrix to display matrix
for ( int i=0; i < mat_x; i++ ) {
for ( int j=0; j < mat_y; j++ ) {
display[i][j] = pos[i][j];
}
}
// rotate
for (float time=0; time < 1000; time+=2) {
// delete previous matrix from HDC
/*for ( int i=0; i < mat_x; i++ ) {
for ( int j=0; j < mat_y; j++ ) {
SetPixel(hdc,display[i][j].x,display[i][j].y,delete_col);
}
}*/
// set the rotation matrix
rotation[0][0] = cos(time);
rotation[0][1] = (-1)*sin(time);
rotation[1][0] = sin(time);
rotation[1][1] = cos(time);
// multiply rotation matrix by position matrix
for ( int i=0; i < mat_x; i++ ) {
for ( int j=0; j < mat_y; j++ ) {
display[i][j].x = (pos[i][j].x*rotation[0][0])+(pos[i][j].y*rotation[0][1]);
display[i][j].y = (pos[i][j].x*rotation[1][0])+(pos[i][j].y*rotation[1][1]);
}
}
// draw the pixels
for ( int i=0; i < mat_x; i++ ) {
for ( int j=0; j < mat_y; j++ ) {
SetPixel(hdc,display[i][j].x,display[i][j].y,draw_col);
}
}
//Sleep(100);
}
}