Last active
February 29, 2020 10:20
-
-
Save jeffery812/4280fbe6494771ff3b03a24ff7f71e33 to your computer and use it in GitHub Desktop.
CGRect centre, largestContainedSquare, smallestContainingSquare
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
import QuartzCore | |
public extension CGRect { | |
public init(centre: CGPoint, size: CGSize) { | |
self.init(origin: centre.applying(CGAffineTransform(translationX: size.width / -2, y: size.height / -2)), size: size) | |
} | |
public var centre: CGPoint { | |
return CGPoint(x: midX, y: midY) | |
} | |
public var largestContainedSquare: CGRect { | |
let side = min(width, height) | |
return CGRect(centre: centre, size: CGSize(width: side, height: side)) | |
} | |
public var smallestContainingSquare: CGRect { | |
let side = max(width, height) | |
return CGRect(centre: centre, size: CGSize(width: side, height: side)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment