-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.elm
More file actions
301 lines (239 loc) · 6.26 KB
/
Copy pathMain.elm
File metadata and controls
301 lines (239 loc) · 6.26 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
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
grid =
rectangularGrid puzzle.piecesX puzzle.piecesY
|> perturbGrid
markers =
Dict.values grid
|> List.map drawMarker
edges =
calcEdges grid
in
canvas params.width
params.height
[ Svg.g [] markers
, Svg.g [] <| List.map drawEdge edges
, drawCurve3 basicTongue
]
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
-- 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
]
[]