Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.6 kB
1
Indexable
Never
import SwiftUI

struct ContentView: View {
    @State private var angleDegrees: Double = 60
    
    var body: some View {
        VStack {
            EquilateralTriangle(angleDegrees: angleDegrees)
                .frame(width: 100, height: 100)
                .padding(.top, 100)
            
            Slider(value: $angleDegrees, in: 0...60, step: 1)
                .padding(.horizontal)
        }
    }
}

struct EquilateralTriangle: Shape {
    var angleDegrees: Double
    
    var animatableData: Double {
        get { angleDegrees }
        set { angleDegrees = newValue }
    }
    
    func path(in rect: CGRect) -> Path {
        let centerX = rect.midX
        let centerY = rect.midY
        let sideLength = min(rect.width, rect.height) / 2.0
        let initialRotationAngle = CGFloat(60 - angleDegrees) * .pi / 180.0
        
        let pointA = CGPoint(x: centerX, y: centerY - sideLength)
        let pointB = CGPoint(
            x: centerX - CGFloat(sin(initialRotationAngle)) * sideLength,
            y: centerY + CGFloat(cos(initialRotationAngle)) * sideLength
        )
        let pointC = CGPoint(
            x: centerX + CGFloat(sin(initialRotationAngle)) * sideLength,
            y: centerY + CGFloat(cos(initialRotationAngle)) * sideLength
        )
        
        var path = Path()
        path.move(to: pointA)
        path.addLine(to: pointB)
        path.addLine(to: pointC)
        path.closeSubpath()
        
        return path
    }
}

@main
struct EquilateralTriangleApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}