I have a javascript Matrix class for affine transforms, and a function to set the rotation to an absolute number of radians, around a given center:

```
this.setRotation = function (radians, center) {
    var cos = Math.cos(radians);
    var sin = Math.sin(radians);
    this.a = cos;
    this.b = sin;
    this.c = -sin;
    this.d = cos;
    this.tx += center.x - center.x * cos + center.y * sin;
    this.ty += center.y - center.x * sin - center.y * cos;
}

```

I'm trying to rotate around the center of the object itself, so I'm passing in a center point of half the object's width, and half its height, so for a 100 x 100 object I'm passing in 50, 50.

If my object starts from a rotation of zero, this works fine:

![Before](https://i.stack.imgur.com/fwclz.png) ![After](https://i.stack.imgur.com/zjg7W.png)

... but if I rotate the shape again, or start with a rotation of other than zero, the tx and ty values end up wrong:

![Position moved](https://i.stack.imgur.com/lo71K.png)

What's wrong with my formula above? Setting the rotation seems to be accurate, but not the tx and ty.

I have seen a few other questions on this subject, in particular [this](https://stackoverflow.com/questions/12049003/the-innards-of-a-rotation-function) one, but nothing that has helped.

**Update** To add some numbers to this:

If I begin with a 100x100 rectangle, positioned at 100,100, then my initial matrix is: `{Matrix: [a:1, b:0, c:0, d:1, tx:100, ty:100]}`

To rotate this clockwise 45 degrees, I feed the above function 0.7853981633974483 (45 degrees in radians), and the center: `{Point: [x:50, y: 50]}` 

This produces the following matrix: 
`{Matrix: [a:0.7071067812, b:0.7071067812, c:-0.7071067812, d:0.7071067812, tx:150, ty:79.28932188]}` which is exactly right.

But if I then start with that matrix, and try to return it to zero, by feeding the function arguments of 0 and `{Point: [x:70.71067812000001, y: 70.71067812000001]}` (the center of the new, rotated shape), then the output is `{Matrix: [a:1, b:0, c:0, d:1, tx:150, ty:79.28932188]}`, which is the correct rotation but not the correct translation. I'm expecting to get `{Matrix: [a:1, b:0, c:0, d:1, tx:100, ty:100]}`

I've tried several other variants of the function, including using the center of the rectangle in the parent coordinate space 150,150 instead of the local center of 50,50, and replacing += with = as suggested below, but nothing seems to help. If I comment out the tx calculation then my shape rotates beautifully around it's origin, so the issue must be either with the tx/ty calculation, or with the center point that I'm passing in.

Does anyone have any further ideas?