path.fit()

將 Path 塞到另一個 CGRect 中。

// ⭐️ required:
//    - 🅿️ Vector2D
//    - 🌀 Path + transform

import SwiftUI
import VectorSpace  // for `-v` additive inverse

extension Path {
    // path.fit(in: rect)
    public func fit(in rect: CGRect) -> Path {
        // scale factors
        let x = rect.width  / boundingRect.width
        let y = rect.height / boundingRect.height
        // fit self into rect
        return self
            .translate(-boundingRect.origin)   // 🅿️ Vector2D, 🌀Path + transform
            .dilate(x, y)                      // 🌀Path + transform
            .translate(rect.origin)
    }
}

Last updated