A faster way, but still not stereotyped, would be to look only at the possible values โโof y, and not at the iteration up to MAX_Y. eg:.
static Size FindMinSize(double requiredRatio, double epsilon) { int x = 1; do { int y = (int)(x * requiredRatio); if (Test(x, y, requiredRatio, epsilon)) { return new Size(x, y); } y = (int)((x + 1) * requiredRatio); if (Test(x, y, requiredRatio, epsilon)) { return new Size(x, y); } x++; } while (x != int.MaxValue); return new Size(0, 0); } static bool Test(int x, int y, double requiredRatio, double epsilon) { double aspectRatio = ((double)y)/x; return Math.Abs(aspectRatio - requiredRatio) < epsilon; }
source share