C# WinForms-一个矩形覆盖另一个矩形的面积百分比

C# WinForms-一个矩形覆盖另一个矩形的面积百分比,c#,system.drawing,rectangles,C#,System.drawing,Rectangles,给定两个System.Drawing.Rectangle-如何确定第二个矩形覆盖第一个矩形面积的百分比 例如,如果第二个矩形位于第一个矩形的一半位置,则结果应为50%。您可以使用矩形.Intersect方法获得交点矩形: Rectangle rect = new Rectangle(firstRect.Location, firstRect.Size); rect.Intersect(secondRectangle); var percentage = (rect.Width * rect.He

给定两个System.Drawing.Rectangle-如何确定第二个矩形覆盖第一个矩形面积的百分比


例如,如果第二个矩形位于第一个矩形的一半位置,则结果应为50%。

您可以使用
矩形.Intersect
方法获得
交点
矩形:

Rectangle rect = new Rectangle(firstRect.Location, firstRect.Size);
rect.Intersect(secondRectangle);
var percentage = (rect.Width * rect.Height) * 100f/(firstRect.Width * firstRect.Height);

您所说的中间位置是什么意思?这里有两个维度:水平维度和垂直维度。不管维度是什么,结果都应该提供第一个矩形的覆盖范围。