I have following piece of code :
You just need to add half the width/height of the rectangle to get its centre.
See transform documentation of the rotate function for more information.