如何使用矩阵旋转矩形并获取修改后的矩形? [英] How can I rotate a rectangle using a matrix and get the modified rectangle?

查看:166
本文介绍了如何使用矩阵旋转矩形并获取修改后的矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了所有链接以进行矩形旋转,但是似乎没有什么适用于我的问题.我有一个RectangleF结构,希望将其输入到旋转矩阵中.然后使用生成的RectangleF传递给其他函数.

I have searched all the links for rectangle rotation, but nothing seems to apply to my problem. I have a RectangleF structure and wish to feed it into a rotation matrix. Then use resulting RectangleF to pass into some other function.

想要使用矩阵的原因是因为我可能还想执行平移,然后执行缩放操作,然后将生成的矩形传递给其他函数,例如

The reason for wanting to use a matrix is because I may also want to perform a translation, and then perhaps a scale afterwards and pass the resultant rectangle into some other function, e.g.

RectangleF original = new RectangleF(0,0, 100, 100);
Matrix m = new Matrix();
m.Rotate(35.0f);
m.Translate(10, 20);

....   (what do I do here ?)

RectangleF modified = (How/where do I get the result?)

SomeOtherFunction(modified);

我该如何实现?

我不想在屏幕或其他任何物体上绘制此矩形.我只需要这些值,但是我读过的所有示例都使用graphics类进行变换和绘制,而这并不是我想要的.

I don't want to draw this rectangle on a screen or anything else. I just need the values, but all examples I have read use the graphics class to do the transform and draw which is not what I want.

非常感谢

推荐答案

System.Drawing.Rectangle结构始终是正交的,不能旋转.您只能旋转其拐角点.

The System.Drawing.Rectangle structure is always orthogonal and cannot have a rotation. You can only rotate its corner points.

以下是使用Matrix进行此操作的示例:

Here is an example of doing this with a Matrix:

Matrix M = new Matrix();

// just a rectangle for testing..
Rectangle R = panel1.ClientRectangle;
R.Inflate(-33,-33);

// create an array of all corner points:
var p = new PointF[] {
    R.Location,
    new PointF(R.Right, R.Top),
    new PointF(R.Right, R.Bottom),
    new PointF(R.Left, R.Bottom) };

// rotate by 15° around the center point:
M.RotateAt(15, new PointF(R.X + R.Width / 2, R.Top + R.Height / 2));
M.TransformPoints(p);

// just a quick (and dirty!) test:
using (Graphics g = panel1.CreateGraphics())
{
    g.DrawRectangle(Pens.LightBlue, R);
    g.DrawPolygon(Pens.DarkGoldenrod, p );
}

诀窍是创建一个PointPointF数组,其中包含您感兴趣的所有点,这里是四个角;然后Matrix可以根据您要求的各种事物来变换这些点,围绕其中一个点旋转.其他包括缩放剪切翻译.

The trick is to create an array of Point or PointF containg all points you are interested in, here the four corners; the Matrix can then transform those points according to all sorts of things you asked for, rotation around a point being one of them. Others include scaling, shearing and translating..

结果如预期:

如果您反复需要此功能,则需要创建将Rectangle转换为Point []并返回的函数.

If you need this repeatedly you will want to create function to convert Rectangle to Point[] and back.

注意,后者实际上是不可能的,因为 Rectangle将始终是正交的,即不能旋转,因此您将拥有去拐角点.或如Quergo在其帖子中所示,从System.Windows名称空间切换到Rect类.

Note, as pointed out above, that the latter is not really possible, as Rectangle will always be orthogonal, i.e. cannot be rotated, so you will have to go for the corner points. Or switch to the Rect class from the System.Windows namespace as Quergo shows in his post.

这篇关于如何使用矩阵旋转矩形并获取修改后的矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆