Pergunta

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

Foi útil?

Solução

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);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top