Skip to content

Instantly share code, notes, and snippets.

@gaboelnuevo
Created September 3, 2015 22:30
Show Gist options
  • Save gaboelnuevo/f7639c69616a50aa1c3d to your computer and use it in GitHub Desktop.
Save gaboelnuevo/f7639c69616a50aa1c3d to your computer and use it in GitHub Desktop.
Calculate a ratio from two numbers
//calculate aspect ratio
// Via: http://stackoverflow.com/questions/1186414/whats-the-algorithm-to-calculate-aspect-ratio-i-need-an-output-like-43-169
function gcd (a, b) {
return (b == 0) ? a : gcd (b, a%b);
}
var w = screen.width;
var h = screen.height;
var r = gcd (w, h);
console.log("Dimensions = " + w + " x " + h);
console.log("Aspect ratio = " + w/r + ":" + h/r);
@jdolearydl
Copy link

Note: This will exceed maximum calls stack size if one of the arguments is NaN.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment