Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. func withinThreshold(rect: CGRect, point: CGPoint, threshhold: CGFloat)-> Bool {
  2. let minX = rect.minX
  3. let maxY = rect.maxY
  4. let maxX = rect.maxX
  5. let minY = rect.minY
  6. let rectWidth = rect.size.width
  7. let rectHeight = rect.size.height
  8. let newX = minX - threshhold
  9. let newY = maxY + threshhold
  10.  
  11. let newWidth = rect.size.width + (threshhold*2)
  12. let newHeight = rect.size.height + (threshhold*2)
  13. let topLeft = CGPoint(x: minX, y: maxY)
  14. let topRight = CGPoint(x: maxX, y: maxY)
  15. let bottomLeft = CGPoint(x: minX, y: minY)
  16. let bottomRight = CGPoint(x: maxX, y: minY)
  17. let corners = [topLeft,topRight,bottomLeft,bottomRight]
  18.  
  19. func CGPointDistanceSquared(from: CGPoint, to: CGPoint) -> CGFloat {
  20. return (from.x - to.x) * (from.x - to.x) + (from.y - to.y) * (from.y - to.y)
  21. }
  22. func CGPointDistance(from: CGPoint, to: CGPoint) -> CGFloat {
  23. return sqrt(CGPointDistanceSquared(from: from, to: to))
  24. }
  25.  
  26. let widthRect = CGRect.init(x: newX, y: maxY, width: newWidth, height: rectHeight)
  27. let heightRect = CGRect.init(x: minX, y: newY, width: rectWidth, height: newHeight)
  28.  
  29. if widthRect.contains(point) || heightRect.contains(point) {
  30. return true
  31. } else {
  32. for corner in corners {
  33. if CGPointDistance(from: corner, to: point) <= threshhold {
  34. return true
  35. }
  36. }
  37. }
  38. return false
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement