I'm really weak with math and having a problem with an image resize algorithm. I'm trying to resize an image to a a specific ratio.
double neededRatio = 1.6d;
while (!AboutEqual(imageRatio, neededRatio))
{
var cropPixels = 10;
//crop code
...
imageRatio = (double)img.Width / img.Height;
}
public static bool AboutEqual(double x, double y)
{
double epsilon = Math.Max(Math.Abs(x), Math.Abs(y)) * 1E-15;
return Math.Abs(x - y) <= epsilon;
}
The problem is, I can't seem to find the right number of pixels to crop to actually make the AboutEqual method work (I found it here). Sometimes it fails and the image get cropped indefinitely, I tried to log the inner-workings of the AboutEqual method and it's showing things I find weird, like:
X: 1.5249500998004 Y: 1.6 result: false
X: 1.55600814663951 Y: 1.6 result: false
X: 1.55600814663951 Y: 1.6 result: false
X: 1.58835758835759 Y: 1.6 result: false
X: 1.62208067940552 Y: 1.6 result: false
X: 1.60084925690021 Y: 1.6 result: false
X: 1.5796178343949 Y: 1.6 result: false
X: 1.61388286334056 Y: 1.6 result: false
X: 1.59219088937093 Y: 1.6 result: false
X: 1.62749445676275 Y: 1.6 result: false
X: 1.60532150776053 Y: 1.6 result: false
X: 1.58314855875831 Y: 1.6 result: false
X: 1.61904761904762 Y: 1.6 result: false
X: 1.59637188208617 Y: 1.6 result: false
X: 1.63341067285383 Y: 1.6 result: false
The linked question says "If both x and y are computed values then you have to increase the epsilon." - How do I do that and find the best number of pixels to crop?
You don't need to iterate to get your value. Calculate the target
xvalue which would give you the desired ratio, and remove extra pixels.Presuming that
xis too large and you want to crop it:[Edit]
The point is, this code gets the target
xyou will need to get to (presuming you need the ratio). If you still thing you need a loop, nothing stops you from doing: