Question

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%.

Était-ce utile?

La solution

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);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top