LoginSignup
5
4

More than 1 year has passed since last update.

2点のCGPointからCGRectを作る

Posted at

任意の2点で構成される四角形を作る場合、よく(p1.x - p0.x, p1.y - p0.y)をsizeとしてCGRectを生成することがあるが、CGRectのunionを利用することで計算を手書きせずに簡単に作ることができる。

let r0 = CGRect(origin: p0, size: .zero)
let r1 = CGRect(origin: p1, size: .zero)
let rect = r0.union(r1).standardized

unionは2つのCGRectを構成する最小のCGRectを生成する関数で、これを利用して座標だけを持つCGRect2つをunionさせて四角形を計算させる。

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4