path.transform

幫 Path 做平移、伸縮、旋轉等變換。

// ⭐️ required: 
//    - 🅿️ Vector2D

import SwiftUI
import VectorSpace  // for 🅿️ Vector2D

extension Path {
    
    /* translation */
    
    // path.translate(x,y)
    public func translate(_ x: CGFloat, _ y: CGFloat) -> Path {
        offsetBy(dx: x, dy: y)
    }
    
    // path.translate(v)
    public func translate<T: Vector2D>(_ v: T) -> Path    // 🅿️ Vector2D
        where T.Field == CGFloat 
    {  
        offsetBy(dx: v.x, dy: v.y)
    }
    
    /* dilation */
    
    // path.dilate(x,y)
    public func dilate(_ x: CGFloat, _ y: CGFloat) -> Path {
        applying(CGAffineTransform(scaleX: x, y: y))
    }
    
    // path.dilate(s)
    public func dilate(_ scale: CGFloat) -> Path {
        dilate(scale, scale)
    }
    
    // path.dilate(v)
    public func dilate<T: Vector2D>(_ v: T) -> Path     // 🅿️ Vector2D
        where T.Field == CGFloat 
    {  
        dilate(v.x, v.y)
    }
}

Last updated