private RectangleF GetRandomImageRect(Database db, Rectangle screenRect, float imgWidth, float imgHeight) { var retries = k_randomRectRetries; var bestRect = new RectangleF(0.0f, 0.0f, 0.0f, 0.0f); var bestOverlap = -1.0f; var screenRectF = screenRect; var oldRects = _theme.ImageRectHistory.ToArray(); while (retries-- > 0) { var rect = new RectangleF( (float)(_rand.NextDouble() * (screenRect.Width - imgWidth)) + screenRect.X, (float)(_rand.NextDouble() * (screenRect.Height - imgHeight)) + screenRect.Y, imgWidth, imgHeight); var overlap = 0.0f; for (int i = 0; i < oldRects.Length; i++) { var ratio = (double)(i + 1) / (double)oldRects.Length; var importance = Math.Pow(ratio, Math.E); overlap += oldRects[i].IntersectArea(rect) * (float)importance; } if (overlap < bestOverlap || bestOverlap < 0) { bestRect = rect; bestOverlap = overlap; } } _theme.AddImageRectHistory(db, bestRect, _screenRects.Count); return(bestRect); }