2013-09-21 52 views
1

给定两个System.Drawing.Rectangle's - 如何确定第二个矩形覆盖的第一个矩形区域的百分比?WinForms - 一个矩形覆盖另一个矩形区域的多少%

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

+0

你说的'中途positioned'是什么意思?我们在这里有2个维度:**水平维度**和**垂直维度**。 –

+0

无论维度如何,结果都应该提供第一个矩形的多少部分。 – SharpAffair

回答

3

可以使用Rectangle.Intersect方法来获取intersection矩形:

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