Repository navigation
Expand file tree
/
Copy pathMain.elm
More file actions
401 lines (320 loc) · 9.02 KB
/
Copy pathMain.elm
File metadata and controls
401 lines (320 loc) · 9.02 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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
module Main exposing (main)
import Dict exposing (Dict)
import List.Extra as List
import Random
import Svg exposing (Svg)
import Svg.Attributes exposing (..)
puzzle =
{ draftMode = True
, piecesX = 12
, piecesY = 6
, pixelsPerCell = 50
, seed = Random.initialSeed 666
, gridPerturb = 6
}
params =
{ width = puzzle.piecesX * puzzle.pixelsPerCell
, height = puzzle.piecesY * puzzle.pixelsPerCell
}
type alias Point =
{ x : Int
, y : Int
}
type alias Edge =
{ start : Point
, end : Point
}
type alias Curve3 =
{ start : Point
, startControl : Point
, middle : Point
, middleControl : Point
, endControl : Point
, end : Point
}
main : Svg msg
main =
let
cnvs =
canvas params.width params.height
grid =
rectangularGrid puzzle.piecesX puzzle.piecesY
|> perturbGrid
markers =
Dict.values grid
|> List.map drawMarker
isOnBorder edge =
False
|| (edge.start.x == 0 && edge.end.x == 0)
|| (edge.start.y == 0 && edge.end.y == 0)
|| (edge.start.x == params.width && edge.end.x == params.width)
|| (edge.start.y == params.height && edge.end.y == params.height)
edges =
calcEdges grid
|> List.filter (not << isOnBorder)
( flips, _ ) =
Random.uniform True [ True, False ]
|> Random.list (List.length edges)
|> (\l -> Random.step l puzzle.seed)
tongues =
List.map2 makeTongue flips edges
border =
Svg.rect
[ x "0"
, y "0"
, width (String.fromInt params.width)
, height (String.fromInt params.height)
, fillOpacity "0"
, stroke "black"
]
[]
in
cnvs <|
if puzzle.draftMode then
[ Svg.g [] <| List.map drawCurve3 tongues
, border
, Svg.g [] markers
, Svg.g [] <| List.map drawEdge edges
]
else
[ Svg.g [] <| List.map drawCurve3 tongues
, border
]
rectangularGrid : Int -> Int -> Dict ( Int, Int ) Point
rectangularGrid nx ny =
let
indicesX =
List.range 0 nx
indicesY =
List.range 0 ny
indices =
List.lift2 Tuple.pair indicesX indicesY
in
indices
|> List.map
(\( ix, iy ) ->
( ( ix, iy )
, { x = ix * puzzle.pixelsPerCell
, y = iy * puzzle.pixelsPerCell
}
)
)
|> Dict.fromList
calcEdges : Dict ( Int, Int ) Point -> List Edge
calcEdges grid =
let
maybeConnect indices point =
Dict.get indices grid
|> Maybe.map (\point2 -> { start = point, end = point2 })
horizontals =
grid
|> Dict.map (\( ix, iy ) point -> maybeConnect ( ix + 1, iy ) point)
|> Dict.toList
|> List.sortBy (\( ( _, iy ), _ ) -> iy)
|> List.map Tuple.second
|> List.filterMap identity
verticals =
grid
|> Dict.map (\( ix, iy ) point -> maybeConnect ( ix, iy + 1 ) point)
|> Dict.toList
|> List.sortBy (\( ( ix, _ ), _ ) -> ix)
|> List.map Tuple.second
|> List.filterMap identity
in
horizontals ++ verticals
perturbGrid : Dict ( Int, Int ) Point -> Dict ( Int, Int ) Point
perturbGrid grid =
let
pert =
puzzle.gridPerturb
randomPair =
Random.pair
(Random.int -pert pert)
(Random.int -pert pert)
randomPairListGen =
Random.list (Dict.size grid) randomPair
( randomPairList, _ ) =
Random.step randomPairListGen puzzle.seed
in
Dict.values grid
|> List.map2 (\( rx, ry ) point -> { x = point.x + rx, y = point.y + ry }) randomPairList
-- optional: keep borders straight
|> List.map snapToBorder
|> List.map2 Tuple.pair (Dict.keys grid)
|> Dict.fromList
snapToBorder : Point -> Point
snapToBorder { x, y } =
{ x = snapToBorder_ puzzle.gridPerturb params.width x
, y = snapToBorder_ puzzle.gridPerturb params.height y
}
snapToBorder_ : Int -> Int -> Int -> Int
snapToBorder_ howClose maxCoord coord =
if coord - howClose <= 0 then
0
else if coord + howClose >= maxCoord then
maxCoord
else
coord
makeTongue : Bool -> Edge -> Curve3
makeTongue flip { start, end } =
let
vEdge =
{ x = end.x - start.x
, y = end.y - start.y
}
vPerp =
if start.y /= end.y then
{ x = 1
, y = toFloat -vEdge.x / toFloat vEdge.y |> round
}
else
--if start.x /= end.x
{ x = toFloat -vEdge.y / toFloat vEdge.x |> round
, y = 1
}
flipMult =
if flip then
-1
else
1
vPerpN =
{ x = flipMult * toFloat vPerp.x / norm vPerp |> round
, y = flipMult * toFloat vPerp.y / norm vPerp |> round
}
middleScale =
toFloat puzzle.pixelsPerCell * 0.18 |> round
scaleV h vect =
{ x = toFloat vect.x * h |> round
, y = toFloat vect.y * h |> round
}
scale h n =
toFloat n * h |> round
middle =
{ x = (vPerpN.x * middleScale) + scale 0.5 (start.x + end.x)
, y = (vPerpN.y * middleScale) + scale 0.5 (start.y + end.y)
}
in
{ start = Point start.x start.y
, startControl = Point (start.x + scale 0.8 vEdge.x) (start.y + scale 0.8 vEdge.y)
, middleControl = Point (middle.x - scale 0.4 vEdge.x) (middle.y - scale 0.4 vEdge.y)
, middle = Point middle.x middle.y
, endControl = Point (end.x - scale 0.8 vEdge.x) (end.y - scale 0.8 vEdge.y)
, end = Point end.x end.y
}
norm : Point -> Float
norm vect =
(vect.x ^ 2 + vect.y ^ 2)
|> toFloat
|> sqrt
-- DRAWING FUNCTIONS
drawMarker : Point -> Svg msg
drawMarker { x, y } =
Svg.circle
[ cx <| String.fromInt x
, cy <| String.fromInt y
, r "2"
, stroke "#666"
, fillOpacity "0"
]
[]
drawEdge : Edge -> Svg msg
drawEdge { start, end } =
Svg.line
[ x1 <| String.fromInt start.x
, y1 <| String.fromInt start.y
, x2 <| String.fromInt end.x
, y2 <| String.fromInt end.y
, strokeWidth "1"
, stroke "#c66"
]
[]
basicTongue : Curve3
basicTongue =
{ start = Point 0 0
, startControl = Point 70 0
, middleControl = Point 10 30
, middle = Point 50 30
, endControl = Point 30 0
, end = Point 100 0
}
drawCurve3 : Curve3 -> Svg msg
drawCurve3 curve =
let
m =
[ curve.start.x
, curve.start.y
]
|> List.map String.fromInt
c =
[ curve.startControl.x
, curve.startControl.y
, curve.middleControl.x
, curve.middleControl.y
, curve.middle.x
, curve.middle.y
]
|> List.map String.fromInt
s =
[ curve.endControl.x
, curve.endControl.y
, curve.end.x
, curve.end.y
]
|> List.map String.fromInt
pathString =
[ "M" :: m, "C" :: c, "S" :: s ]
|> List.map (String.join " ")
|> String.join ""
in
Svg.path
[ stroke "black"
, fill "none"
, d pathString
]
[]
-- CANVAS HELPER
canvas : Int -> Int -> List (Svg msg) -> Svg msg
canvas w h children =
let
hStr =
String.fromInt h
wStr =
String.fromInt w
tileSize =
10
xnumtiles =
w // tileSize
ynumtiles =
h // tileSize
tiles =
List.lift2 (tile tileSize)
(List.range 0 <| xnumtiles - 1)
(List.range 0 <| ynumtiles - 1)
in
Svg.svg
[ width wStr
, height hStr
, viewBox <| "0 0 " ++ wStr ++ " " ++ hStr
]
(if puzzle.draftMode then
Svg.g [] tiles :: children
else
children
)
tile : Int -> Int -> Int -> Svg msg
tile size xc yc =
let
col =
if modBy 2 (xc + yc) == 0 then
"#eee"
else
"#fff"
in
Svg.rect
[ x (String.fromInt (xc * size))
, y (String.fromInt (yc * size))
, width (String.fromInt size)
, height (String.fromInt size)
, fill col
]
[]