我已经搜索了所有链接以进行矩形旋转,但是似乎没有什么适用于我的问题。我有一个RectangleF结构,希望将其输入到旋转矩阵中。然后使用生成的RectangleF传递给其他函数。
想要使用矩阵的原因是因为我可能还要执行翻译,然后再进行缩放,然后将生成的矩形传递给其他函数,例如
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类来进行变换和绘制,而这并不是我想要的。
非常感谢
该System.Drawing.Rectangle
结构始终是正交的,并不能旋转。您只能旋转其拐角点。
以下是使用进行此操作的示例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 );
}
诀窍是创建一个包含Point
或PointF
包含您感兴趣的所有点的数组,这里是四个角;Matrix
然后,您可以根据您要求的各种事物来变换这些点,围绕一个点旋转就是其中之一。其他包括缩放,剪切和平移。
结果与预期的一样:
如果反复需要此功能,则需要创建将Rectangle转换为Point []并返回的函数。
请注意,如上所述,后者实际上是不可能的,因为它Rectangle
始终是正交的,即不能旋转,因此您必须寻找角点。或Rect
从System.Windows
Quergo在其帖子中显示的名称空间切换到该类。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句