GDI+ Rotate rectangle about its center

Hi,

I'm trying to simply rotate a shape about its centre or a specific point but I can not seem to get the right result. My code is below...

1
2
3
4
5
6
7
8
9
10
Graphics g(pDC->GetSafeHdc());
	SolidBrush brush(Color(200,88,88,88));
	int x = 100, y = 100;
	int w = 50, h = 80;
	g.FillRectangle(&brush, x, y, w, h); // Original Rectangle

	g.TranslateTransform(x+(w/2), y+(y/2), MatrixOrderAppend); // Fix position
	g.RotateTransform(45); // Rotate 45 degrees
	g.FillRectangle(&brush, x, y, w, h); // Rotated Rectangle
	g.ResetTransform();


It seems to be rotating around a different point instead of the center of my rectangle. I must be misunderstanding the TranslateTransform part. Can anyone see what I am doing wrong?
g.TranslateTransform(x+(w/2), y+(y/2), MatrixOrderAppend); // Fix position
Shouldn't this be
g.TranslateTransform(x+(w/2), y+(h/2), MatrixOrderAppend); // Fix position
Yes, sorry, typo. I think I sorted it now.
1
2
3
4
5
6
SolidBrush brush(Color(200,88,88,88));	
	g.FillRectangle(&brush, x, y, w, h);
	g.TranslateTransform(x+(w/2), y+(h/2), MatrixOrderAppend); // Move origin
	g.RotateTransform(45); // Rotate 45 degrees
	SolidBrush brush2(Color(88,00,00,99));
	g.FillRectangle(&brush2, -(w/2), -(h/2), w, h);

It gets a bit confusing moving the origin around though. if anyone knows of a better way, please let me know
Last edited on
Topic archived. No new replies allowed.