Skip to content

Instantly share code, notes, and snippets.

@morganwilde
Created March 21, 2015 11:57
Show Gist options
  • Save morganwilde/6fda2ade3bbaf7bbf816 to your computer and use it in GitHub Desktop.
Save morganwilde/6fda2ade3bbaf7bbf816 to your computer and use it in GitHub Desktop.
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