Advertisement
HXXXXJ

Get Most Rect

Feb 21st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.85 KB | None | 0 0
  1. struct Point{
  2.     var x : Int
  3.     var y : Int
  4.     init(_ x : Int, _ y : Int){
  5.         self.x = x
  6.         self.y = y
  7.     }
  8. }
  9.  
  10.  
  11. func getRect(_ arr:[Point] ,  _ center: Point) -> (Int, Int){ //width, height
  12.     var leftDiff = 0
  13.     var rightDiff = 0
  14.     var topDiff = 0
  15.     var bottomDiff = 0
  16.     for p in arr {
  17.         if p.x > center.x {
  18.             rightDiff = max(rightDiff, p.x - center.x)
  19.         }else{
  20.             leftDiff = max(rightDiff, center.x - p.x)
  21.         }
  22.         if p.y > center.y {
  23.             topDiff = max(topDiff, p.y - center.y)
  24.         }else{
  25.             bottomDiff = max(bottomDiff, center.y - p.y)
  26.         }
  27.     }
  28.     return (max(rightDiff, leftDiff) * 2 , max(topDiff, bottomDiff) * 2 )
  29. }
  30.  
  31.  
  32. let p1 = Point(0 , 0)
  33. let p2 = Point(0 , 1)
  34. let arr = [p1,p2]
  35. let center = Point(1 , 1)
  36.  
  37. print(getRect(arr, center))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement