-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWorms.swift
365 lines (295 loc) · 10.7 KB
/
Worms.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Just copy and paste the code into a blank playground template
// in Swift Playgrounds app on an iPad or a Mac
// !!! Turn off Enable Results in the settings, otherwise you'll get an error !!!
// Created by Roman Gaditskiy: https://GitHub.com/gadirom/Art-in-Swift
import SwiftUI
import PlaygroundSupport
import Combine
import CoreGraphics
let context = CIContext(options:nil)
let blurRadius = 10.0
var contentFrame = CGRect()
let constrainStickMinLength = false
let pointSize = CGSize(width: 3, height: 3)
let frameScale: CGFloat = 20
let power: CGFloat = 1
var width: CGFloat = 0.005
var segment: CGFloat = 0
let gravity: CGFloat = 0
let speed: CGFloat = 0.1
var friction: CGFloat = 1
let numberOfWorms = 100
let segmentsInWorm = 10
let wormSpeed: CGFloat = 1
var points: [Point] = []
var sticks: [Stick] = []
var startTime = Date()
func generateWorms(){
_ = (0..<numberOfWorms).map{ w in
let pos = CGPoint(x: CGFloat.random(in: 0..<contentFrame.maxX/2) + contentFrame.midX/2,
y: CGFloat.random(in: 0..<contentFrame.maxY/2) + contentFrame.midY/2)
let speed = CGPoint(x: CGFloat.random(in: -1...1) * wormSpeed, y: CGFloat.random(in: -1...1) * wormSpeed)
points.append(Point(pos: pos, prevPos: speed, locked:true))
var color = CGColor.random()
_ = (0..<segmentsInWorm).map{ seg in
let pos = CGPoint(x: CGFloat.random(in: 0..<contentFrame.maxX/2) + contentFrame.midX/2,
y: CGFloat.random(in: 0..<contentFrame.maxY/2) + contentFrame.midY/2)
points.append(Point(pos: pos, prevPos: pos))
let p = w * (segmentsInWorm+1) + seg
color = color.copy(alpha: (CGFloat(segmentsInWorm)-sqrt(CGFloat(seg)))/CGFloat(segmentsInWorm))!
sticks.append(Stick(pointA: p, pointB: p+1, color: color))
}
}
//sticks.shuffle()
}
extension CGColor{
static func random()->CGColor {
let red = CGFloat.random(in: 0.5...1)
let green = CGFloat.random(in: 0.5...1)
let blue = CGFloat.random(in: 0.5...1)
return CGColor(red: red, green: green, blue: blue, alpha: 1)
}
}
func simulateWorms(time: CGFloat){
DispatchQueue.concurrentPerform(iterations: numberOfWorms){
var worm = points[$0*(segmentsInWorm+1)]
worm.pos += worm.prevPos
worm.prevPos.x -= pow(frameScale / (contentFrame.maxX-worm.pos.x), power)
worm.prevPos.y -= pow(frameScale / (contentFrame.maxY-worm.pos.y), power)
worm.prevPos.x += pow(frameScale / (worm.pos.x), power)
worm.prevPos.y += pow(frameScale / (worm.pos.y), power)
//ordinary bouncing
/*if worm.pos.x > contentFrame.maxX || worm.pos.x < 0 { worm.prevPos.x *= -1 }
if worm.pos.y > contentFrame.maxY || worm.pos.y < 0 { worm.prevPos.y *= -1 }*/
points[$0*(segmentsInWorm+1)] = worm
}
}
func simulate(time: CGFloat) {
DispatchQueue.concurrentPerform(iterations: points.count){
var point = points[$0]
if !point.locked{
let prevPos = point.pos
point.pos = point.pos +
(point.pos - point.prevPos) * friction +
(CGSize(width: 0, height: 1) * gravity * time * time)
point.prevPos = prevPos
}
points[$0] = point
}
for i in 0...0{ // try increasing the range for more uniform results
sticks = sticks.map { stick in
var stickCentre = (points[stick.pointA].pos + points[stick.pointB].pos) / 2.0
var stickDir = (points[stick.pointA].pos - points[stick.pointB].pos).normalized()
var length = (points[stick.pointA].pos - points[stick.pointB].pos).length()
if length != segment {
if (!points[stick.pointA].locked)
{
points[stick.pointA].pos = stickCentre + stickDir * segment / 2
}
if (!points[stick.pointB].locked)
{
points[stick.pointB].pos = stickCentre - stickDir * segment / 2
}
}
return stick
}
}
}
struct Point {
var pos: CGPoint = CGPoint()
var prevPos: CGPoint = CGPoint()
var locked = false
}
struct Stick {
init (pointA: Int, pointB: Int, color: CGColor){
self.pointA = pointA
self.pointB = pointB
self.origLength = segment
self.color = color
}
var pointA: Int
var pointB: Int
var length: CGFloat{ (points[pointB].pos-points[pointA].pos).length() }
var origLength: CGFloat
var color: CGColor
}
func drawPoint(ctx: UIGraphicsRendererContext, origin: CGPoint, size: CGSize, locked: Bool){
let rectangle = CGRect(origin: origin - (size / 2), size: size)
ctx.cgContext.setFillColor(locked ? UIColor.white.cgColor : UIColor.red.cgColor)
ctx.cgContext.setStrokeColor(UIColor.clear.cgColor)
//ctx.cgContext.setLineWidth(5*width)
ctx.cgContext.addEllipse(in: rectangle)
ctx.cgContext.drawPath(using: .fillStroke)
}
func drawImage(size: CGSize, points: [Point], sticks: [Stick]) -> UIImage {
let renderer = UIGraphicsImageRenderer(size: size)
let img = renderer.image { ctx in
var maxLength = size.length()
_ = sticks.map{ stick in
var lineWidth = width * maxLength / stick.length
lineWidth = lineWidth < pointSize.width ? lineWidth : pointSize.width
ctx.cgContext.setLineWidth(lineWidth)
ctx.cgContext.setStrokeColor(stick.color)
ctx.cgContext.move(to: points[stick.pointA].pos)
ctx.cgContext.addLine(to: points[stick.pointB].pos)
ctx.cgContext.drawPath(using: .stroke)
}
//Draw heads
/*for i in 0..<numberOfWorms{
var point = points[i*(segmentsInWorm+1)]
drawPoint(ctx: ctx, origin: point.pos, size: pointSize, locked: point.locked)
}*/
}
return img
}
struct ContentView: View {
init (refreshRate: Double){
self.refreshInterval = 1 / refreshRate
self.timer = Timer.publish(every: refreshInterval, tolerance: 0, on: .current, in: .common).autoconnect()
}
var timer: Publishers.Autoconnect<Timer.TimerPublisher>
let refreshInterval: Double
@State var drawnImage = UIImage()
@State var fric: CGFloat = 0.5
@State var seg: CGFloat = 0
@State var wid: CGFloat = 0.01
@State var simulation = true
var body: some View{
VStack{
ZStack{
Image(uiImage: drawnImage)
Color.clear.contentShape(Rectangle())
.onAppear(){
generateWorms()
}
.onTapGesture(count: 2){
simulation.toggle()
}
}
.onReceive(timer) { time in
friction = fric * 2
segment = seg * 500
width = wid
if simulation{
let t = CGFloat(time.compare(startTime).rawValue)*speed
simulateWorms(time: t)
simulate(time: t)
}else{startTime = time}
drawnImage = drawImage(size: contentFrame.size, points: points, sticks: sticks)
}
.overlay(
GeometryReader { geo in
Color.clear
.preference(key: framePreferenceKey.self, value: geo.frame(in:.global))
}.onPreferenceChange(framePreferenceKey.self){contentFrame = $0}
)
Slider(value: $fric)
Slider(value: $seg)
Slider(value: $wid)
}
}
}
struct framePreferenceKey: PreferenceKey {
static var defaultValue = CGRect()
static func reduce(value: inout CGRect, nextValue: () -> CGRect) {
value = nextValue()
}
}
extension CGPoint{
static func +(lhs: CGPoint, rhs: CGSize) -> CGPoint{
CGPoint(
x: lhs.x + rhs.width,
y: lhs.y + rhs.height
)
}
static func +(lhs: CGPoint, rhs: CGPoint) -> CGPoint{
CGPoint(
x: lhs.x + rhs.x,
y: lhs.y + rhs.y
)
}
static func +=(lhs: inout CGPoint, rhs: CGPoint) {
lhs = CGPoint(
x: lhs.x + rhs.x,
y: lhs.y + rhs.y
)
}
static func +=(lhs: inout CGPoint, rhs: CGSize) {
lhs = CGPoint(
x: lhs.x + rhs.width,
y: lhs.y + rhs.height
)
}
static func -(lhs: CGPoint, rhs: CGSize) -> CGPoint{
CGPoint(
x: lhs.x - rhs.width,
y: lhs.y - rhs.height
)
}
static func -(lhs: CGPoint, rhs: CGPoint) -> CGSize{
CGSize(
width: lhs.x - rhs.x,
height: lhs.y - rhs.y
)
}
static func /(lhs: CGPoint, rhs: CGFloat) -> CGPoint {
CGPoint(x: lhs.x / rhs,
y: lhs.y / rhs)
}
func length() -> CGFloat{
sqrt(x * x + y * y)
}
func normalized() -> CGPoint {
let len = length()
return len>0 ? self / len : .zero
}
}
extension CGSize{
func length() -> CGFloat{
sqrt(width * width + height * height)
}
func normalized() -> CGSize {
let len = length()
return len>0 ? self / len : .zero
}
mutating func rotate(_ a: CGFloat) {
let x = width * cos(a) - height * sin(a)
let y = width * sin(a) + height * cos(a)
self = CGSize(width: x, height: y)
}
mutating func randomizeAngle(_ rnd: CGFloat) {
let a = CGFloat.random(in: -rnd...rnd)
self.rotate(a)
}
static func +=(lhs: inout CGSize, rhs: CGSize) {
lhs = CGSize(
width: lhs.width + rhs.width,
height: lhs.height + rhs.height
)
}
static func +(lhs: CGSize, rhs: CGSize) -> CGSize{
CGSize(
width: lhs.width + rhs.width,
height: lhs.height + rhs.height
)
}
static func -(lhs: CGSize, rhs: CGSize) -> CGSize{
CGSize(
width: lhs.width - rhs.width,
height: lhs.height - rhs.height
)
}
static func *(lhs: CGSize, rhs: CGFloat) -> CGSize {
CGSize(width: lhs.width * rhs,
height: lhs.height * rhs)
}
static func +(lhs: CGSize, rhs: CGFloat) -> CGSize {
CGSize(width: lhs.width + rhs,
height: lhs.height + rhs)
}
static func /(lhs: CGSize, rhs: CGFloat) -> CGSize {
CGSize(width: lhs.width / rhs,
height: lhs.height / rhs)
}
}
PlaygroundPage.current.setLiveView(ContentView(refreshRate: 120))