Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func withinThreshold(rect: CGRect, point: CGPoint, threshhold: CGFloat)-> Bool {
- let minX = rect.minX
- let maxY = rect.maxY
- let maxX = rect.maxX
- let minY = rect.minY
- let rectWidth = rect.size.width
- let rectHeight = rect.size.height
- let newX = minX - threshhold
- let newY = maxY + threshhold
- let newWidth = rect.size.width + (threshhold*2)
- let newHeight = rect.size.height + (threshhold*2)
- let topLeft = CGPoint(x: minX, y: maxY)
- let topRight = CGPoint(x: maxX, y: maxY)
- let bottomLeft = CGPoint(x: minX, y: minY)
- let bottomRight = CGPoint(x: maxX, y: minY)
- let corners = [topLeft,topRight,bottomLeft,bottomRight]
- func CGPointDistanceSquared(from: CGPoint, to: CGPoint) -> CGFloat {
- return (from.x - to.x) * (from.x - to.x) + (from.y - to.y) * (from.y - to.y)
- }
- func CGPointDistance(from: CGPoint, to: CGPoint) -> CGFloat {
- return sqrt(CGPointDistanceSquared(from: from, to: to))
- }
- let widthRect = CGRect.init(x: newX, y: maxY, width: newWidth, height: rectHeight)
- let heightRect = CGRect.init(x: minX, y: newY, width: rectWidth, height: newHeight)
- if widthRect.contains(point) || heightRect.contains(point) {
- return true
- } else {
- for corner in corners {
- if CGPointDistance(from: corner, to: point) <= threshhold {
- return true
- }
- }
- }
- return false
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement