forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler.elm
More file actions
332 lines (252 loc) · 7.27 KB
/
euler.elm
File metadata and controls
332 lines (252 loc) · 7.27 KB
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
module Euler exposing (..)
import Html exposing (Html, div, button, text, h3)
import Html.Attributes exposing (style)
import Html.Events exposing (onClick, on)
import Time exposing (Time, second)
import Maybe exposing (withDefault)
import Window exposing (Size, size)
import Svg exposing (svg, circle, line, polyline)
import Svg.Attributes exposing (width, height, stroke, x1, x2, y1, y2, cx, cy, r, points, fill)
import Task exposing (perform)
import Slider exposing (..)
import Mouse
import Json.Decode as Decode
import Hex
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Model =
{ part : Particle
, dt : Time
, dt0 : Time
, t : Time
, status : Status
, wWidth : Int
, wHeight : Int
, history : List ( Time, Time, Particle )
, drag : Maybe Drag
}
type alias Position =
Float
type alias Velocity =
Float
type alias Particle =
{ pos : List Position, vel : List Velocity }
type Status
= Idle
| Running
type alias Drag =
{ start : Position
, current : Position
}
getX : Particle -> Position
getX p =
withDefault 0 <| List.head <| .pos p
getV : Particle -> Velocity
getV p =
withDefault 0 <| List.head <| .vel p
getX0 : Model -> Position
getX0 m =
let
scale x =
3 - 6 * x / (toFloat m.wHeight)
in
case m.drag of
Nothing ->
getX m.part
Just { start, current } ->
getX m.part + scale current - scale start
-- INIT
init : ( Model, Cmd Msg )
init =
( Model (Particle [ x0 ] [ 0 ]) 0.5 0.5 0 Idle 0 0 [] Nothing, perform GetSize size )
x0 : Position
x0 =
2.5
-- UPDATE
type Msg
= Start
| Stop
| Tick Time
| GetSize Size
| SliderUpdate Float
| DragStart Mouse.Position
| DragAt Mouse.Position
| DragEnd Mouse.Position
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Start ->
( { model
| status = Running
, t = 0
, dt = model.dt0
, drag = Nothing
}
, Cmd.none
)
Stop ->
( { model
| status = Idle
, part = Particle [ x0 ] [ 0 ]
, t = 0
}
, Cmd.none
)
Tick _ ->
case model.status of
Idle ->
( model, Cmd.none )
Running ->
if model.t > 5 + model.dt then
( { model
| status = Idle
, part = Particle [ x0 ] [ 0 ]
, history = ( model.dt, model.t, model.part ) :: model.history
, t = 0
}
, Cmd.none
)
else
( { model
| part = evolve model.part model.t model.dt
, t = model.t + model.dt
}
, perform GetSize size
)
GetSize s ->
( { model | wWidth = s.width, wHeight = s.height * 8 // 10 }, Cmd.none )
SliderUpdate dt ->
( { model | dt0 = dt }, Cmd.none )
DragStart { x, y } ->
case model.status of
Idle ->
( { model | drag = Just (Drag (toFloat y) (toFloat y)) }, Cmd.none )
Running ->
( model, Cmd.none )
DragAt { x, y } ->
( { model | drag = Maybe.map (\{ start } -> Drag start (toFloat y)) model.drag }
, Cmd.none
)
DragEnd _ ->
( { model
| drag = Nothing
, part = Particle [ getX0 model ] [ k * getX0 model ]
}
, Cmd.none
)
k : Float
k =
-2
diffEq : Position -> Velocity -> Time -> Time -> ( Position, Velocity )
diffEq x v t dt =
( x + (k * x) * dt, k * (x + (k * x) * dt) )
evolve : Particle -> Time -> Time -> Particle
evolve p t dt =
let
( x, v ) =
diffEq (getX p) (getV p) t dt
in
{ p | pos = x :: p.pos, vel = v :: p.vel }
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
case model.drag of
Nothing ->
Time.every (model.dt * second) Tick
Just _ ->
Sub.batch [ Mouse.moves DragAt, Mouse.ups DragEnd ]
-- VIEW
view : Model -> Html Msg
view model =
div []
[ h3 [] [ text "Drag the ball up or down, pick a dt and click Start" ]
, h3 [ style [ ( "color", gradient model.dt0 ) ] ]
[ viewSlider
, text ("dt = " ++ toString model.dt0)
, button [ onClick Start ] [ text "Start" ]
, button [ onClick Stop ] [ text "Stop" ]
]
, svg
[ width (toString model.wWidth)
, height (toString model.wHeight)
, stroke "black"
]
([ line
[ x1 "0"
, x2 (toString model.wWidth)
, y1 (toString (model.wHeight // 2))
, y2 (toString (model.wHeight // 2))
]
[]
, line
[ x1 (toString (model.wWidth // 20))
, x2 (toString (model.wWidth // 20))
, y1 "0"
, y2 (toString model.wHeight)
]
[]
, viewCircle model
]
++ (plotHistory model)
)
]
viewSlider : Html Msg
viewSlider =
props2view [ MinVal 0, MaxVal 1, Step 0.01, onChange SliderUpdate ]
scaleX : Int -> Position -> String
scaleX h x =
toString (toFloat h / 2 * (1 - x / 3))
scaleT : Int -> Time -> String
scaleT w t =
toString (toFloat w * (0.05 + t / 5))
viewCircle : Model -> Html Msg
viewCircle m =
circle
[ cy (scaleX m.wHeight (getX0 m))
, cx (scaleT m.wWidth m.t)
, r "10"
, on "mousedown" (Decode.map DragStart Mouse.position)
]
[]
plotPath : Int -> Int -> ( Time, Time, Particle ) -> String
plotPath w h ( dt, tf, particle ) =
let
comb x ( t, s ) =
( t - dt, s ++ (scaleT w t) ++ "," ++ (scaleX h x) ++ " " )
in
Tuple.second <| List.foldl comb ( tf, "" ) particle.pos
plotHistory : Model -> List (Html Msg)
plotHistory m =
let
( w, h ) =
( m.wWidth, m.wHeight )
in
List.map
(\( dt, t, p ) ->
polyline
[ stroke "black"
, fill "none"
, stroke (gradient dt)
, points (plotPath w h ( dt, t, p ))
]
[]
)
(( m.dt, m.t, m.part ) :: m.history)
gradient : Time -> String
gradient dt =
let
( r, g, b ) =
( round (255 * dt), 0, round (255 * (1 - dt)) )
col =
Hex.toString (256 * (256 * r + g) + b)
in
if String.length col < 6 then
"#" ++ String.repeat (6 - String.length col) "0" ++ col
else
"#" ++ col