💜Vehicle.CarBodyShape

// 2022.02.10
import SwiftUI
extension Vehicle {
    /// `Vehicle.CarBodyShape`
    struct CarBodyShape: Shape {
        
        // relative points in rect
        let A : UnitPoint = [0  , 1  ]        // 🅿️ Vector2D
        let B : UnitPoint = [1  , 0.8]
        let C1: UnitPoint = [0.1, 0.4]        // control points
        let C2: UnitPoint = [0.2, 0.5]
        
        let C : UnitPoint = [0.8, 1]
        let C3: UnitPoint = [0.9, 1]          // control points
        let C4: UnitPoint = [1  , 1]
        
        func path(in rect: CGRect) -> Path {
            Path { path in
                // move to A
                path.move(to: rect[A])        // 🅿️ Rectangular
                // add curve to B
                path.addCurve(to: rect[B], control1: rect[C1], control2: rect[C2])
                // add curve to C
                path.addCurve(to: rect[C], control1: rect[C3], control2: rect[C4])
                // add line back to A
                path.closeSubpath()
            }
        }
    }
}// ⭐️ previews 叫什麼名字好像沒關係❓ 
struct CarBodyShape_Previews: PreviewProvider {
    static var previews: some View {
        let b = Vehicle.CarBodyShape()
        b
            // ⭐️ set primary color for shape
            .foregroundColor(.green.opacity(0.8))
            .frame(height: 150)
            .overlay {
                // 🖼 Pin (4 control points)
                Pin(b.C1, b.A)
                Pin(b.C2, b.B)
                Pin(b.C4, b.C, .blue)
                Pin(b.C3, b.B, .blue)
                // 🖼 Point (3 key points)
                Point(b.A, .gray, label: "A")
                Point(b.B, .gray, label: "B")
                Point(b.C, .gray, label: "C")
            }
            .background(GridLines())            // 🖼 GridLines
            .border(Color.blue.opacity(0.3))
            .padding(40)
    }
}Last updated
Was this helpful?