« Air bug? minHeight/minWidth | Home | Telefónica and Microsoft against Skype »

October 23, 2008

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

1 Comment

alguien debe limpiar estos comentarios... eso que dice en chino no es muy bueno :P

About this Entry

This page contains a single entry by fernando published on October 23, 2008 12:14 PM.

Air bug? minHeight/minWidth was the previous entry in this blog.

Telefónica and Microsoft against Skype is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.