使用C#实现矩形相对于旋转矩形的旋转

我有两个矩形:

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

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

获取原始图像:

https://img3.mukewang.com/64e16ea6000133e504600477.jpg

在图像编辑器中进行了所述修改:

https://img3.mukewang.com/64e16eb30001c3b603440435.jpg

有必要对矩形重复这些操作,这是我的代码:


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);

}

结果:

https://img3.mukewang.com/64e16ec1000184ff02820360.jpg

我在某个地方弄错了,父母匹配,但孩子不匹配。也许一切都在计算父偏移量时崩溃了?



忽然笑
浏览 371回答 1
1回答

眼眸繁星

我发现了几个问题:首先- 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 canvasvar 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 locationchildren.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 centervar rotateChildrenMatrix = new Matrix();rotateChildrenMatrix.RotateAt(childrenAngle, parentCenter);rotateChildrenMatrix.TransformPoints(childrenVertices);// rotate by canvas centervar 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);}结果:
打开App,查看更多内容
随时随地看视频慕课网APP