// // Pie.swift // Memorize // // Created by ching on 2023/2/15. // import SwiftUI struct Pie: Shape { var startAngle: Angle var endAngle: Angle var clockwise = false var animatableData: AnimatablePair { get { AnimatablePair(startAngle.radians, endAngle.radians) } set { startAngle = Angle.radians(newValue.first) endAngle = Angle.radians(newValue.second) } } func path(in rect: CGRect) -> Path { let center = CGPoint(x: rect.midX, y: rect.midY) let radius = min(rect.height, rect.width) / 2 let start = CGPoint( x: center.x + radius * CGFloat(cos(startAngle.radians)), y: center.y + radius * CGFloat(sin(startAngle.radians)) ) var p = Path() p.move(to: center) p.addLine(to: start) p.addArc( center: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: !clockwise ) p.addLine(to: center) return p } }