-
Notifications
You must be signed in to change notification settings - Fork 367
/
Logo.elm
381 lines (300 loc) · 7.78 KB
/
Logo.elm
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
module Logo exposing
( Model
, start
, view
, setPattern
, step
, perturb
, isMoving
, Pattern
, logo
, heart
, bird
, child
, house
, cat
, camel
)
import Cycle
import Html
import Svg exposing (Svg, polygon, svg, g)
import Svg.Attributes exposing (fill, height, points, transform, viewBox)
-- MODEL
type alias Model =
{ tb1 : Shape
, tb2 : Shape
, tm : Shape
, sqr : Shape
, par : Shape
, ts1 : Shape
, ts2 : Shape
}
type Shape
= Static Float Float Float
| Moving Float Float Float Float Float Float Float Float Float
start : Model
start =
{ tb1 = Static 0 -210 0
, tb2 = Static -210 0 90
, tm = Static 207 207 45
, sqr = Static 150 0 0
, par = Static -89 239 0
, ts1 = Static 0 106 180
, ts2 = Static 256 -150 270
}
-- VIEW
view : List (Html.Attribute msg) -> Model -> Html.Html msg
view attrs model =
svg
(viewBox "-600 -600 1200 1200" :: attrs)
[ g [ transform "scale(1 -1)"
]
[ viewShape "rgba(18, 147, 216, 1)" model.tb1 triangleBig
, viewShape "rgba(18, 147, 216, 1)" model.tb2 triangleBig
, viewShape "rgba(18, 147, 216, 0.75)" model.tm triangleMedium
, viewShape "rgba(18, 147, 216, 1)" model.sqr square
, viewShape "rgba(18, 147, 216, 0.75)" model.par parallelogram
, viewShape "rgba(18, 147, 216, 0.75)" model.ts1 triangleSmall
, viewShape "rgba(18, 147, 216, 0.75)" model.ts2 triangleSmall
]
]
viewShape : String -> Shape -> String -> Svg msg
viewShape color shape coordinates =
case shape of
Static x y a -> viewShapeHelp color x y a coordinates
Moving _ _ _ x y a _ _ _ -> viewShapeHelp color x y a coordinates
viewShapeHelp : String -> Float -> Float -> Float -> String -> Svg msg
viewShapeHelp color x y a coordinates =
polygon
[ fill color
, points coordinates
, transform <|
"translate(" ++ String.fromFloat x ++ " " ++ String.fromFloat y
++ ") rotate(" ++ String.fromFloat -a ++ ")"
]
[]
triangleBig = "-280,-90 0,190 280,-90" -- /\ -- 396x396x560
triangleMedium = "-198,-66 0,132 198,-66" -- / \ -- 280x280x396
triangleSmall = "-130,-44 0,86 130,-44" -- /____\ -- 184x184260
square = "-130,0 0,-130 130,0 0,130" -- 184x184
parallelogram = "-191,61 69,61 191,-61 -69,-61"
-- SET PATTERN
setPattern : Pattern -> Model -> Model
setPattern target model =
{ tb1 = setShape target.tb1 model.tb1
, tb2 = setShape target.tb2 model.tb2
, tm = setShape target.tm model.tm
, sqr = setShape target.sqr model.sqr
, par = setShape target.par model.par
, ts1 = setShape target.ts1 model.ts1
, ts2 = setShape target.ts2 model.ts2
}
setShape : Target -> Shape -> Shape
setShape {tx,ty,ta} shape =
case shape of
Static x y a ->
Moving 0 0 0 x y a tx ty ta
Moving vx vy va x y a _ _ _ ->
Moving vx vy va x y a tx ty ta
-- STEP
step : Float -> Model -> Model
step timeDelta model =
let
dt = timeDelta / 1000
in
{ tb1 = stepShape dt model.tb1
, tb2 = stepShape dt model.tb2
, tm = stepShape dt model.tm
, sqr = stepShape dt model.sqr
, par = stepShape dt model.par
, ts1 = stepShape dt model.ts1
, ts2 = stepShape dt model.ts2
}
stepShape : Float -> Shape -> Shape
stepShape dt shape =
case shape of
Static _ _ _ ->
shape
Moving vx vy va x y a tx ty ta ->
let
tt = toNearestAngle a ta
ax = pstiffness * (tx - x) - pdamping * vx
ay = pstiffness * (ty - y) - pdamping * vy
at = astiffness * (tt - a) - adamping * va
nvx = vx + ax * dt
nvy = vy + ay * dt
nva = va + at * dt
nx = x + nvx * dt
ny = y + nvy * dt
na = a + nva * dt
dx = abs (tx - nx)
dy = abs (ty - ny)
da = abs (tt - na)
in
if dx < 1 && dy < 1 && da < 1 && abs nvx < 0.6 && abs nvy < 0.6 && abs nva < 0.6 then
Static tx ty (normalize ta)
else
Moving nvx nvy nva nx ny na tx ty ta
toNearestAngle : Float -> Float -> Float
toNearestAngle current target =
let
distance = target - current
in
if distance < -180 then
target + 360
else if distance > 180 then
target - 360
else
target
normalize : Float -> Float
normalize angle =
if angle < 0 then
angle + 360
else if 360 < angle then
angle - 360
else
angle
pstiffness = 220
pdamping = 10
astiffness = 200
adamping = 10
-- PERTURB
-- TODO figure out how to get angle to center of mass involved
--
perturb : Float -> Float -> Float -> Float -> Float -> Model -> Model
perturb timeDelta x y dx dy model =
if timeDelta > 40 then
model
else
let
dt = max 1 timeDelta / 40000
in
{ tb1 = perturbShape dt x y dx dy model.tb1
, tb2 = perturbShape dt x y dx dy model.tb2
, tm = perturbShape dt x y dx dy model.tm
, sqr = perturbShape dt x y dx dy model.sqr
, par = perturbShape dt x y dx dy model.par
, ts1 = perturbShape dt x y dx dy model.ts1
, ts2 = perturbShape dt x y dx dy model.ts2
}
perturbShape : Float -> Float -> Float -> Float -> Float -> Shape -> Shape
perturbShape dt mx my dx dy shape =
case shape of
Static x y a ->
let
rx = mx * 10 - x / 120
ry = my * 10 - y / 120
r2 = max 0.5 (rx * rx + ry * ry)
vx = dx / (dt * r2)
vy = dy / (dt * r2)
in
Moving vx vy 0 x y a x y a
Moving vx vy va x y a tx ty ta ->
let
rx = mx * 10 - x / 120
ry = my * 10 - y / 120
r2 = max 0.5 (rx * rx + ry * ry)
nvx = vx + dx / (dt * r2)
nvy = vy + dy / (dt * r2)
in
Moving nvx nvy va x y a tx ty ta
-- IS MOVING?
isMoving : Model -> Bool
isMoving model =
isMovingShape model.tb1
|| isMovingShape model.tb2
|| isMovingShape model.tm
|| isMovingShape model.sqr
|| isMovingShape model.par
|| isMovingShape model.ts1
|| isMovingShape model.ts2
isMovingShape : Shape -> Bool
isMovingShape shape =
case shape of
Static _ _ _ -> False
Moving _ _ _ _ _ _ _ _ _ -> True
-- PATTERNS
type alias Pattern =
{ tb1 : Target
, tb2 : Target
, tm : Target
, sqr : Target
, par : Target
, ts1 : Target
, ts2 : Target
}
type alias Target =
{ tx : Float
, ty : Float
, ta : Float
}
logo : Pattern
logo =
{ tb1 = Target 0 -210 0
, tb2 = Target -210 0 90
, tm = Target 207 207 45
, sqr = Target 150 0 0
, par = Target -89 239 0
, ts1 = Target 0 106 180
, ts2 = Target 256 -150 270
}
heart : Pattern
heart =
{ tb1 = Target -160 120 0
, tb2 = Target 150 -90 180
, tm = Target -270 -93 45
, sqr = Target 0 -300 0
, par = Target 231 91 0
, ts1 = Target 150 224 0
, ts2 = Target -106 -150 90
}
bird : Pattern
bird =
{ tb1 = Target -296 166 45
, tb2 = Target 0 40 225
, tm = Target 200 136 270
, sqr = Target -42 -212 45
, par = Target -138 -424 135
, ts1 = Target 139 -181 315
, ts2 = Target 352 214 225
}
child : Pattern
child =
{ tb1 = Target -88 -46 135
, tb2 = Target 208 86 -45
, tm = Target 120 -300 0
, sqr = Target 104 352 36
, par = Target -140 -300 315
, ts1 = Target -404 -380 315
, ts2 = Target 328 -434 180
}
house : Pattern
house =
{ tb1 = Target 0 -250 0
, tb2 = Target 96 54 0
, tm = Target -218 -152 315
, sqr = Target -106 266 45
, par = Target -212 56 315
, ts1 = Target 162 -104 180
, ts2 = Target 264 -206 270
}
cat : Pattern
cat =
{ tb1 = Target -40 -120 90
, tb2 = Target 20 -420 135
, tm = Target -226 -38 270
, sqr = Target -220 276 0
, par = Target 350 -462 315
, ts1 = Target -320 428 90
, ts2 = Target -120 428 270
}
camel : Pattern
camel =
{ tb1 = Target -250 -256 315
, tb2 = Target 100 -260 270
, tm = Target -190 -30 0
, sqr = Target 40 40 0
, par = Target 278 40 90
, ts1 = Target 262 276 90
, ts2 = Target 366 380 180
}