TITLE: How do I rotate a matrix transformation with a centered origin?
QUESTION [5 upvotes]: This is actually something I'm doing in Objective-C programming, but since it's very math-oriented I thought I'd post it here.
I was reading up on linear transformations: http://en.wikipedia.org/wiki/Matrix_(mathematics)
Basically I need to rotate a shape around its center. With my current implementation, the rotation is done using the top left as its origin. Here's a screenshot:

Here's the code I'm using to create each x,y coordinate:

CGFloat angle = 0.261799; // (15 degrees)
CGFloat xr = x1 * cosf(angle) + y1 * -sinf(angle) + tx;
CGFloat yr = x1 * sinf(angle) + y1 * cosf(angle) + ty;

I realize there are functions (CGAffineTransformRotate, etc) in Objective-C that do this for me, but since I'm not using a UIView, I need to do it manually. Plus, it would be nice to know. :)
So when I plug in 35.0 for tx and -35.0 for ty, (numbers I just found that seemed to work, through trial and error) here is what I get:

That's what I want! Now I just need some way to figure out these tx and ty translation values to center the shape's origin, based on the given angle, width or height values. (I'm also seeing different results when setting tx and ty if the original x,y coordinates of the shape are different.)
Any help would be much appreciated. Thanks!

REPLY [2 votes]: As Jiri pointed out your problem is easy to solve once you have found the "center of gravity". It can be quite tricky for complicated examples but here what you have to do is:

Fix the origin of the frame
Draw the shape
Find the coordinates of the center-of-gravity
Use this coordinate to translate your control points (e.g. corners for rectangle) to the origin
Perform the rotation on the control points and translate back. 

Here is a 3D-Computer graphics link and some nice code that popped up in a quick search.