首页 > 解决方案 > 使用 C# 相对于旋转矩形旋转矩形

问题描述

我有两个矩形:

首先parent相对于画布中心旋转 -15 度

接下来children相对于画布的中心旋转 -15 度并相对于parent.

获取原始图像: 在此处输入图像描述

在图像编辑器中进行了描述的修改: 在此处输入图像描述

有必要用 rectangles 重复这些操作,这是我的代码:

var parentAngle = -15;
var childrenAngle = 5;

var parent = new Rectangle(new Point(50, 160), new Size(200, 300));
var children = new Rectangle(new Point(25, 175), new Size(50, 50));

// load transformed file to as canvas
var bmp = Image.FromFile(@"D:\Temp\transform.png");

var size = bmp.Size;

var canvasCenter = new PointF(size.Width / 2, size.Height / 2);
var parentCenter = new PointF(parent.Location.X + parent.Width / 2, parent.Location.Y + parent.Height / 2);
var parentLocation = parent.Location;

var parentVertices = parent.GetVertices();
var childrenVertices = children.GetVertices();

// rotate by canvas center
var rotateMatrix = new Matrix();
rotateMatrix.RotateAt(parentAngle, canvasCenter);
rotateMatrix.TransformPoints(parentVertices);

// rotate children vertices
var rotateMatrix2 = new Matrix();
rotateMatrix2.RotateAt(childrenAngle, parentCenter);
rotateMatrix2.TransformPoints(childrenVertices);

 // translate vertices
var translateMatrix = new Matrix();
translateMatrix.Translate(parentLocation.X, parentLocation.Y);
translateMatrix.TransformPoints(childrenVertices);

// rotate by canvas center
rotateMatrix.TransformPoints(childrenVertices);


using (Graphics g = Graphics.FromImage(bmp))
{
    g.DrawPolygon(Pens.Green, parentVertices);
    g.DrawPolygon(Pens.Blue, childrenVertices);
}

结果在此处输入图像描述

我在某个地方弄错了,父母匹配但孩子不匹配。也许一切都在计算父偏移量处发生故障?

更新:GetVertices 函数被实现为助手,如下所示:

    public static PointF[] GetVertices(this Rectangle rect)
    {
        return new[] {
            rect.Location,
            new PointF(rect.Right, rect.Top),
            new PointF(rect.Right, rect.Bottom),
            new PointF(rect.Left, rect.Bottom)
        };
    }

标签: c#rotationgeometry

解决方案


我发现了几个问题:

首先-paint.net 相对于画布中心旋转选定的图层。因此,什么都没有,只好重新绘制测试用例 在此处输入图像描述

接下来- 我不得不重新计算将孩子的位置转移到顶部。现在看起来像这样:

var parentAngle = -15;
var childrenAngle = 5;

var parent = new Rectangle(new Point(50, 160), new Size(200, 300));
var children = new Rectangle(new Point(25, 175), new Size(50, 50));

// load transformed file to as canvas
var bmp = Image.FromFile(@"D:\Temp\rotate_5.png");

var size = bmp.Size;

var canvasCenter = new PointF(size.Width / 2, size.Height / 2);
var parentLocation = parent.Location;
var parentCenter = new PointF(parentLocation.X + parent.Width / 2, parentLocation.Y + parent.Height / 2);
var childrenLocation = children.Location;

// translate location children by parent location
children.Location = childrenLocation = new Point(parentLocation.X + childrenLocation.X, childrenLocation.Y + parentLocation.Y);
var childrenCenter = new PointF(childrenLocation.X + children.Width / 2, childrenLocation.Y + children.Height / 2);

var parentVertices = parent.GetVertices();
var childrenVertices = children.GetVertices();

//rotate by canvas center
var rotateChildrenMatrix = new Matrix();
rotateChildrenMatrix.RotateAt(childrenAngle, parentCenter);
rotateChildrenMatrix.TransformPoints(childrenVertices);

// rotate by canvas center
var rotateMatrix = new Matrix();
rotateMatrix.RotateAt(parentAngle, canvasCenter);

rotateMatrix.TransformPoints(parentVertices);
rotateMatrix.TransformPoints(childrenVertices);

using (Graphics g = Graphics.FromImage(bmp))
{
    g.DrawPolygon(Pens.Green, parentVertices);
    g.DrawPolygon(Pens.Blue, childrenVertices);
}

结果在此处输入图像描述


推荐阅读