亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用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

接下來- 我必須重新計算將孩子的位置轉移到頂部?,F在看起來像這樣:


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 關注
  • 514 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號