Last active
August 29, 2015 14:06
-
-
Save HiromichiYamada/610b244b362b71f4a59d to your computer and use it in GitHub Desktop.
Calculate Screen Ratios.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Euclidean algorithm for GCD | |
func euclid( a:Int, b:Int ) -> Int{ | |
var aa : Int = a | |
var bb : Int = b | |
var rr : Int = aa % bb | |
while rr != 0 { | |
aa = bb | |
bb = rr | |
rr = aa % bb | |
} | |
return bb | |
} | |
func calcRatio( a:Int, b:Int ) -> ( ra:Int, rb:Int ){ | |
let gcd : Int = euclid(a, b) | |
return (a/gcd, b/gcd) | |
} | |
func printRatio( a:Int, b:Int, memo:String? = nil ){ | |
let ratio = calcRatio(a, b) | |
let prefex = (memo != nil) ? "\(memo!) : " : "" | |
println("\(prefex)[\(a):\(b)] => [\(ratio.0):\(ratio.1)](\(Float(ratio.0)/Float(ratio.1)))") | |
} | |
// iPhone Screen Ratios | |
printRatio(640, 960, memo:"iPhone 4 (S)") // [2:3](0.666666686534882) | |
printRatio(640, 1136, memo:"iPhone 5 (C|S)") // [40:71](0.563380300998688) | |
printRatio(750, 1334, memo:"iPhone 6") // [375:667](0.562218904495239) | |
printRatio(1080, 1920, memo:"iPhone 6 Plus") // [9:16](0.5625) | |
printRatio(1536, 2048, memo:"iPad (all)") // [3:4](0.75) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment