Created
March 21, 2015 11:57
-
-
Save morganwilde/6fda2ade3bbaf7bbf816 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
func getMinDistanceBetweenRectangles(a: CGRect, b: CGRect) -> CGFloat { | |
let deltaX = b.origin.x - a.origin.x | |
let deltaY = b.origin.y - a.origin.y | |
let delta = abs(deltaX) > abs(deltaY) ? deltaX : deltaY | |
var distance: CGFloat = 0 | |
switch (delta >= 0, delta == deltaX) { | |
case (true, true) : distance = b.origin.x - (a.origin.x + a.width) | |
case (true, false) : distance = b.origin.y - (a.origin.y + a.height) | |
case (false, true) : distance = a.origin.x - (b.origin.x + b.width) | |
case (false, false) : distance = a.origin.y - (b.origin.y + b.height) | |
default: println("error") | |
} | |
return distance | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment