为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
忽然笑 2023-08-20 09:38:07
我有两个矩形:首先parent相对画布中心旋转-15度接下来children相对于画布的中心旋转-15度,相对于 的中心旋转5度parent。获取原始图像:在图像编辑器中进行了所述修改:有必要对矩形重复这些操作,这是我的代码: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\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 centervar rotateMatrix = new Matrix();rotateMatrix.RotateAt(parentAngle, canvasCenter);rotateMatrix.TransformPoints(parentVertices);// rotate children verticesvar rotateMatrix2 = new Matrix();rotateMatrix2.RotateAt(childrenAngle, parentCenter);rotateMatrix2.TransformPoints(childrenVertices); // translate verticesvar translateMatrix = new Matrix();translateMatrix.Translate(parentLocation.X, parentLocation.Y);translateMatrix.TransformPoints(childrenVertices);// rotate by canvas centerrotateMatrix.TransformPoints(childrenVertices);using (Graphics g = Graphics.FromImage(bmp)){    g.DrawPolygon(Pens.Green, parentVertices);    g.DrawPolygon(Pens.Blue, childrenVertices);}结果:我在某个地方弄错了,父母匹配,但孩子不匹配。也许一切都在计算父偏移量时崩溃了?
查看完整描述

1 回答

?
眼眸繁星

TA贡献1873条经验 获得超9个赞

我发现了几个问题:

首先- Paint.net 相对于画布中心旋转选定的图层。因此,没有任何结果,必须重新绘制测试用例

https://img1.sycdn.imooc.com//64e16ed0000118c402910395.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\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);

}

结果:

https://img1.sycdn.imooc.com//64e16edf0001176d02750377.jpg

查看完整回答
反对 回复 2023-08-20
  • 1 回答
  • 0 关注
  • 367 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信