Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Point{
- var x : Int
- var y : Int
- init(_ x : Int, _ y : Int){
- self.x = x
- self.y = y
- }
- }
- func getRect(_ arr:[Point] , _ center: Point) -> (Int, Int){ //width, height
- var leftDiff = 0
- var rightDiff = 0
- var topDiff = 0
- var bottomDiff = 0
- for p in arr {
- if p.x > center.x {
- rightDiff = max(rightDiff, p.x - center.x)
- }else{
- leftDiff = max(rightDiff, center.x - p.x)
- }
- if p.y > center.y {
- topDiff = max(topDiff, p.y - center.y)
- }else{
- bottomDiff = max(bottomDiff, center.y - p.y)
- }
- }
- return (max(rightDiff, leftDiff) * 2 , max(topDiff, bottomDiff) * 2 )
- }
- let p1 = Point(0 , 0)
- let p2 = Point(0 , 1)
- let arr = [p1,p2]
- let center = Point(1 , 1)
- print(getRect(arr, center))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement