🌀CGRect

這個擴展幫 CGRect 新增一些好用的屬性。

目前版本

/*
 * ⭐️ required: 
 *    - 🅿️ Rectangular
 */

import SwiftUI       // for CGRect

// 🌀CGRect + Rectangular
extension CGRect: Rectangular { }

// 🌀CGRect + convenience inits
extension CGRect {
    
    /// CGRect(x,y,w,h)
    public init(_ x: CGFloat, _ y: CGFloat, _ width: CGFloat, _ height: CGFloat) {
        self.init(x: x, y: y, width: width, height: height)
    }
    
    /// rect from one corner to another.
    /// `CGRect(from: A, to: B)`
    public init(from p1: CGPoint, to p2: CGPoint) {
        let x = p1.x
        let y = p1.y
        let w = p2.x - p1.x
        let h = p2.y - p1.y
        self.init(x: x, y: y, width: w, height: h)
    }
    
    /// rect with center and corner.
    /// - `CGRect(center: A, corner: B)`
    public init(center: CGPoint, corner: CGPoint) {
        let x = corner.x
        let y = corner.y
        let dx = center.x - corner.x
        let dy = center.y - corner.y
        self.init(x: x, y: y, width: 2 * dx, height: 2 * dy)
    }
    
    /* ------ Modifiers -------- */
    
    /// - turn a `CGRect` into `Path` 
    public var path: Path { Path(self) }
    
}

修改記錄

  • 2020.09.30:➕ added .path

  • 2020.10.08: ➕ .maxSide ✏️ minLengthminSide CGRect(x, y, w, h) ➕ subscript: rect[x, y]

  • ➊ 2020.10.11:將大部分的功能移至 Rectangular

Last updated