旋转屏幕上的矩形

发布于 2025-01-07 12:07:36 字数 354 浏览 4 评论 0原文

我正在使用 GDI+ 尝试在屏幕上绘制一个矩形并将其旋转 45 度。
这是我正在使用的代码

Pen RedPen(Color(255, 255, 0, 0), 4);

HDC screenDC = GetDC(NULL);
Graphics graphics(screenDC);

graphics.RotateTransform(45);
graphics.DrawRectangle(&RedPen, 150, 150, 50, 50);

矩形会旋转,但旋转得越多,它的位置就会以圆形移动。
我很确定这是因为我正在旋转屏幕的中心,而不是矩形的中心?
那么我该如何围绕矩形的中心旋转它呢?

I'm using GDI+ to try to draw a rectangle on the screen and rotate it 45 degrees.
Here's the code I'm using

Pen RedPen(Color(255, 255, 0, 0), 4);

HDC screenDC = GetDC(NULL);
Graphics graphics(screenDC);

graphics.RotateTransform(45);
graphics.DrawRectangle(&RedPen, 150, 150, 50, 50);

The rectangle rotates, but it's position moves in a circle the more it rotates.
I'm pretty sure this is because I'm rotating the center of the screen, instead of the center of the rectangle?
So how would I rotate it around the center of the rectangle?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

套路撩心 2025-01-14 12:07:36

问题是它没有像您注意到的那样围绕矩形的中心旋转。因此,您需要在旋转对象后对其进行平移。

        e->Graphics->RotateTransform(degrees);
        e->Graphics->TranslateTransform(posX, posY, MatrixOrder::Append);
        e->Graphics->DrawRectangle(gcnew Pen( Color::Blue,3.0f ),  -width / 2, -height / 2, width, height);

是您想要旋转矩形的量。 posXposY 是您要在屏幕上绘制它的位置。

此外,您还需要确保传递 MatrixOrder::Append,否则变换的顺序可能会更改,并且将在旋转之前应用平移(给您带来与您所看到的类似的效果)

The problem is that it's not rotating around the center of the Rectangle as you noticed. So you need to translate the object after it's rotated.

        e->Graphics->RotateTransform(degrees);
        e->Graphics->TranslateTransform(posX, posY, MatrixOrder::Append);
        e->Graphics->DrawRectangle(gcnew Pen( Color::Blue,3.0f ),  -width / 2, -height / 2, width, height);

degrees is the amount you want to rotate your rectangle. posX and posY are the position where you want to draw it in screen.

Also you need to make sure you pass MatrixOrder::Append otherwise the order of the Transform might be changed and that will apply the Translation before rotating (giving you a similar effect to what you are seeing)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文