Calculate aspect ratio based on width and height
For a project i’m working on i had the need to calculate the aspect ratio par (4:3, 16:9, etc.) based on the width and height entered.
Santiago Anglés once again helped me out. He pointed me to this wikipedia entry: Euclidean Algorithm that let me get the greatest common divisor very easily and with that the rest was simple
- private function getMCD(w:int, h:int):int{
- return ((h != 0) ? arguments.callee(h, w % h): w);
- }
- public function getAspectRatio(w:uint, h:uint):String{
- var mcd:Number = getMCD(w, h);
- return [(w / mcd), (h / mcd)].join(":");
- }
And here some tests:
- trace (getAspectRatio(320, 240)); // 4:3
- trace (getAspectRatio(540, 480)); // 9:8
- trace (getAspectRatio(550, 400)); // 11:8
Thanks Buddy!
—fernando
Erik Porroa
alguien debe limpiar estos comentarios… eso que dice en chino no es muy bueno :P