문제

Given two System.Drawing.Rectangle's - how to determine what % of the first rectangle's area the second rectangle covers?

For example, if the second rectangle is half-way positioned to the first one, the result should be 50%.

도움이 되었습니까?

해결책

You can use the Rectangle.Intersect method to get the intersection rectangle:

Rectangle rect = new Rectangle(firstRect.Location, firstRect.Size);
rect.Intersect(secondRectangle);
var percentage = (rect.Width * rect.Height) * 100f/(firstRect.Width * firstRect.Height);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top