Mode:
Duration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import SwiftUI
struct ContentView: View {
func branch(_ path: inout Path,_ p: CGPoint,_ len: CGFloat,_ angle: Double,_ depth: Int){
guard depth > 0 else { return }
let end = CGPoint(x: p.x + cos(angle) * len, y: p.y - sin(angle) * len)
path.move(to: p)
path.addLine(to: end)
branch(&path,end,len*0.7,angle+0.4,depth-1)
branch(&path,end,len*0.7,angle-0.4,depth-1)
}
var body: some View {
Canvas { context,size in
var path = Path()
branch(&path,CGPoint(x:size.width/2,y:size.height-20),100,.pi/2,8)
context.stroke(path,with:.color(.green),lineWidth:2)
}
}
}Coding works best on desktop or with an external keyboard.