Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save digiguru/a2882cada80afb1d0062 to your computer and use it in GitHub Desktop.
Save digiguru/a2882cada80afb1d0062 to your computer and use it in GitHub Desktop.
/* euclidean GCD (feel free to use any other) */
function gcd(a,b) {if(b>a) {temp = a; a = b; b = temp} while(b!=0) {m=a%b; a=b; b=m;} return a;}
/* ratio is to get the gcd and divide each component by the gcd, then return a string with the typical colon-separated value */
function ratio(x,y) {c=gcd(x,y); return ""+(x/c)+":"+(y/c)}
/* fix it so the shortest side is always first */
function rotatedRatio(x,y) {var bx=x;if(x>y){x=y;y=bx;} return ratio(x,y)}
/* eg:
> rotatedRatio(320,240)
"3:4"
> rotatedRatio(240,320)
"3:4"
> rotatedRatio(360,240)
"2:3"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment