Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate the aspect ratio by a given factor?

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
like image 900
MC Emperor Avatar asked Sep 16 '11 08:09

MC Emperor


People also ask

What is the formula for calculating aspect ratio?

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.

How do you calculate 16:9 aspect ratio?

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.

What ratio is 8.5 by 11?

Another popular print size, 8.5×11 inches is a standard document size in the U.S. The corresponding ratio is 8.5:11.

How do you find the aspect ratio of a 4 5?

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.


2 Answers

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.

like image 178
Chad Greenburg Avatar answered Nov 03 '22 00:11

Chad Greenburg


Disclaimer: These algorithms are silly and inefficient. I'm sure there's a better one...

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

Update: Alternative algorithm

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 :-)

like image 31
Lukas Eder Avatar answered Nov 02 '22 23:11

Lukas Eder