Skip to content

Instantly share code, notes, and snippets.

@FlexMonkey
Last active January 22, 2016 05:32
Show Gist options
  • Save FlexMonkey/f108cadba2a128dcb514 to your computer and use it in GitHub Desktop.
Save FlexMonkey/f108cadba2a128dcb514 to your computer and use it in GitHub Desktop.
CGRect extension gets and sets a rectangle's corners
extension CGRect
{
/// Return corners in order of top left, top right, bottom left, bottom right
var corners: [CGPoint]
{
return [
CGPoint(x: minX, y: minY),
CGPoint(x: maxX, y: minY),
CGPoint(x: minX, y: maxY),
CGPoint(x: maxX, y: maxY)
]
}
/// Sets corner position at given index based on `corners`
mutating func setCornerAtIndex(index: Int, position: CGPoint)
{
switch index
{
case 0:
size.width = size.width + (origin.x - position.x)
size.height = size.height + (origin.y - position.y)
origin.x = position.x
origin.y = position.y
case 1:
size.width = position.x - origin.x
size.height = size.height + (origin.y - position.y)
origin.y = position.y
case 2:
size.width = size.width + (origin.x - position.x)
size.height = position.y - origin.y
origin.x = position.x
case 3:
size.width = position.x - origin.x
size.height = position.y - origin.y
default:
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment