It clocks in at about 8us, which is pretty good.
Can you think of any way to improve the performance?
One thing I don't like about it is the rather large if statements.
Do you think the sacrifice in clarity is worth the slimming of the code?
It makes sense for this method to take two Size objects.
I felt like this complicated the method call to much.
Would switching the parameters to two Size objects improve this method?
Code: Select all
public static Size GetSizeWithinBounds(Int32 height, Int32 width, Int32 max_height, Int32 max_width)
{
Int32 new_height = height;
Int32 new_width = width;
Int32 height_difference = height - max_height;
Int32 width_difference = width - max_width;
if (height_difference > 0 && (width_difference <= 0 || height_difference >= width_difference))
{
new_height = max_height;
new_width = (Int32)Math.Round((Double)new_height / ((Double)height / (Double)width), 0);
}
else if (width_difference > 0 && (height_difference <= 0 || width_difference >= height_difference))
{
new_width = max_width;
new_height = (Int32)Math.Round((Double)new_width * ((Double)height / (Double)width), 0);
}
return new Size(new_width, new_height);
}