public void rotate(double theta)

        // this is the matrix to multiply by..
        //x*cos(theta) , y* sin(theta) 
        //  x* -sin(theta)
        //y * cos(theta)

        vertices[0][0]=  vertices[0][0]* Math.cos(theta)+  Math.sin(theta)*vertices[0][1];
        vertices[0][1]=  vertices[0][0] * (-Math.sin(theta)) +  Math.cos(theta)*vertices[0][1];

        vertices[1][0]=  vertices[1][0]* Math.cos(theta)+ Math.sin(theta)*vertices[1][1];
        vertices[1][1]=  vertices[1][0] * (-Math.sin(theta)) + Math.cos(theta)*vertices[1][1];

        vertices[2][0]=  vertices[2][0]* Math.cos(theta)+ Math.sin(theta)*vertices[2][1];
        vertices[2][1]=  vertices[2][0] * (-Math.sin(theta)) +   Math.cos(theta)*vertices[2][1];

        vertices[3][0]=  vertices[3][0]* Math.cos(theta)+ Math.sin(theta)*vertices[3][1];
        vertices[3][1]=  vertices[3][0] * (-Math.sin(theta)) +  Math.cos(theta)*vertices[3][1] ;
2 Years
Discussion Span
Last Post by michael.james.90475

I want to rotate a simple square object by a passed number of degrees by using a rotation matrix. I have gotten to a point where although it rotates, it has a very wide rotation field, it's more of an orbit than a rotation it's not quite working! I wonder does anyone have any ideas what's wrong?


I guess that rotates about the 0,0 origin of your coordinate system.

If you want to rotate about (say) the center of the object you can:
translate the object so the centre of the object is at the origin
rotate the object
translate it back to the original position
(translation is a trival add/subtract to every vertex)

there may be a smarter way, but I'm no expert.


My panel is 500 x 500, and my object starts bang in the middle before rotation, so I presume this is 250,250 on the axis.
I have a transform method, so what you're saying is I need to transform by 250,250 to move the object to where I actually have it in the window, and then after rotation, transform -250,-250 to go back to my object origon?


I had this problem a while back using AffineTransforms, and solved it like that. Eg if you want to rotate about the center of the object, move it so its center is at the center of rotation (normlly 0,0 of your coordinate system), rotarte it, then reverse the original translation to put the object back where it belongs.
With your figures that would be
translate -100, -100
translate 100, 100

But I'm no expert, and there's probably a better way that I don't know about.

Edited by JamesCherrill


Well I'll give that a try, it sounds about right as I've researched the rotations quite a lot, and it talks of the order of transformations, I never knew what they meant but I guess if you have 2 transforms and a rotation, then order would come into it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.