SwiftyJD
SwiftyJD

Reputation: 5441

How to calculate height/width based on CGPoint positions

I have an array of 3 CGPoints, what I'm trying to do is create a CGRect based on those CGPoint coordinates. I need this CGRect to set the bounds of a view so it can "zoom in" to those coordinates as soon as the view is loaded, but I'm not sure how to get the height and width. Any help is greatly appreciated!

Upvotes: 0

Views: 268

Answers (1)

Adrian Bobrowski
Adrian Bobrowski

Reputation: 2794

let points: [CGPoint] = [
    .init(x: 1, y: 1), 
    .init(x: 5, y: 1), 
    .init(x: 8, y: 5)
]

let bounds = points.reduce(CGRect.null) { rect, point in
    return rect.union(CGRect(origin: point, size: .zero))
}

Upvotes: 0

Related Questions