How do I calculate the aspect ratio (formatted as integer:integer
) by a given factor?
For example, aspect ratio 16:9 has a factor of 1.778, because 16 / 9 = 1.778. But how can I find the ratio by that factor? So
Dimension getAspectRatio(double factor) {
...
}
public static void main(String[] arguments) {
Dimension d = getAspectRatio(16d / 9d);
System.out.println(d.width + ":" + d.height);
}
should return
16:9
To create an exact aspect ratio, divide the height by the width. For example: 2:3 aspect ratio: 3 ÷ 2 = 1.5, so you'd drag the slider to 150.
16:9 ratios are an aspect ratio containing 16 units of width for every 9 units of height. They can be calculated by ensuring that width divided by height equals 1.78.
Another popular print size, 8.5×11 inches is a standard document size in the U.S. The corresponding ratio is 8.5:11.
Aspect ratios are often represented as two numbers separated by a colon, e.g. 4:5. This can be a convenient way of displaying an aspect ratio and can be converted into a numerical value by dividing the first by the second. So, for example, an aspect ratio written as 4:5 has a value of 4/5 = 0.8.
This is an extremely late reply, but I have solved this using a much easier way and I know other's would appreciate it.
I'm assuming that you already know the screen resolution since you know the aspect ratio (decimal equivalent). You can find the aspect ratio (integer:integer) by solving for the greatest common factor between the screen width and height.
public int greatestCommonFactor(int width, int height) {
return (height == 0) ? width : greatestCommonFactor(height, width % height);
}
This will return the greatest common factor between the screen width and height. To find the actual aspect ratio, you just divide the screen width and height by the greatest common factor. So...
int screenWidth = 1920;
int screenHeight = 1080;
int factor = greatestCommonFactor(screenWidth, screenHeight);
int widthRatio = screenWidth / factor;
int heightRatio = screenHeight / factor;
System.out.println("Resolution: " + screenWidth + "x" + screenHeight;
System.out.println("Aspect Ratio: " + widthRatio + ":" + heightRatio;
System.out.println("Decimal Equivalent: " + widthRatio / heightRatio;
This outputs:
Resolution: 1920x1080
Aspect Ratio: 16:9
Decimal Equivalent: 1.7777779
Hope this helps.
Note: This won't work for some resolutions. Comments contain more info.
A silly, straightforward (not very efficient) algorithm to find an approximation is this:
double ratio = 1.778;
double bestDelta = Double.MAX_VALUE;
int bestI = 0;
int bestJ = 0;
for (int i = 1; i < 100; i++) {
for (int j = 1; j < 100; j++) {
double newDelta = Math.abs((double) i / (double) j - ratio);
if (newDelta < bestDelta) {
bestDelta = newDelta;
bestI = i;
bestJ = j;
}
}
}
System.out.println("Closest ratio: " + bestI + "/" + bestJ);
System.out.println("Ratio : " + ((double) bestI / (double) bestJ));
System.out.println("Inaccurate by: " + bestDelta);
Output.
Closest ratio: 16/9
Ratio : 1.7777777777777777
Inaccurate by: 2.2222222222234578E-4
I've just thought of an alternative algorithm, which tries to close in on the approximation. Of course, it's still not very efficient...
double bestDelta = Double.MAX_VALUE;
int i = 1;
int j = 1;
int bestI = 0;
int bestJ = 0;
for (int iterations = 0; iterations < 100; iterations++) {
double delta = (double) i / (double) j - ratio;
// Optionally, quit here if delta is "close enough" to zero
if (delta < 0) i++;
else j++;
double newDelta = Math.abs((double) i / (double) j - ratio);
if (newDelta < bestDelta) {
bestDelta = newDelta;
bestI = i;
bestJ = j;
}
}
System.out.println("Closest ratio: " + bestI + "/" + bestJ);
System.out.println("Ratio : " + ((double) bestI / (double) bestJ));
System.out.println("Inaccurate by: " + bestDelta);
The output is the same
Should I stumble upon an efficient algorithm, I'll post it here :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With