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
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:
Output.
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…
The output is the same
Should I stumble upon an efficient algorithm, I’ll post it here 🙂