int best_h; int last_sum = MAX_TILE_HEIGHT; for (int h = min_h; h <= max_h; h++) { int sum = 0; for (int c = 0; c < lengthof(corners); c++) { sum += abs(h - corners[c]); } if (sum < last_sum) { last_sum = sum; best_h = h; } }