Вопрос

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