-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain.elm
More file actions
363 lines (284 loc) · 8.66 KB
/
Copy pathMain.elm
File metadata and controls
363 lines (284 loc) · 8.66 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
module Main exposing (main)
import Angle
import Array
import Axis2d
import BoundingBox2d
import CubicSpline2d exposing (CubicSpline2d)
import Direction2d
import Geometry.Svg
import LineSegment2d exposing (LineSegment2d)
import List.Extra as List
import Point2d
import Polygon2d
import Quantity exposing (Unitless)
import Random
import Result
import Svg exposing (..)
import Svg.Attributes exposing (..)
import Vector2d
import VoronoiDiagram2d
puzzle =
{ piecesX = 18
, piecesY = 13
, gridPerturb = 3
, seed = Random.initialSeed 768
, draftMode = True
}
params =
{ width = puzzle.piecesX * 40
, height = puzzle.piecesY * 40
}
main : Svg msg
main =
let
cnvs =
canvas params.width params.height
pointCoords =
-- randomGrid params.width params.height 400
perturbedRectangular puzzle.piecesX puzzle.piecesY puzzle.gridPerturb
markers =
List.map marker pointCoords
coordToPoint ( xc, yc ) =
Point2d.unitless (toFloat xc) (toFloat yc)
voronoi =
Array.fromList pointCoords
|> Array.map coordToPoint
|> VoronoiDiagram2d.fromPoints
|> Result.withDefault VoronoiDiagram2d.empty
polygons =
voronoi
|> VoronoiDiagram2d.polygons
(BoundingBox2d.from
(Point2d.unitless 0 0)
(Point2d.unitless params.width params.height)
)
lineCoord : LineSegment2d Unitless coordinates -> ( Int, Int )
lineCoord lineSegment =
-- a little "trick" to compare edges for equality
-- independent of orientation: just compare their midpoint
LineSegment2d.midpoint lineSegment
|> Point2d.toUnitless
|> (\{ x, y } -> ( round x, round y ))
edges =
List.map Tuple.second polygons
|> List.concatMap Polygon2d.edges
|> List.uniqueBy lineCoord
edgesLong =
List.filter (\l -> Quantity.toFloat (LineSegment2d.length l) >= 20) edges
edgesShort =
List.filter (\l -> Quantity.toFloat (LineSegment2d.length l) < 20) edges
drawingLong =
List.map
(Geometry.Svg.lineSegment2d [ stroke "#999", strokeDasharray "2", fillOpacity "0" ])
edgesLong
drawingShort color =
List.map
(Geometry.Svg.lineSegment2d [ stroke color, fillOpacity "0" ])
edgesShort
edgesLongInner =
List.filter (not << isOnBorder) edgesLong
( flips, _ ) =
Random.uniform True [ True, False ]
|> Random.list (List.length edgesLongInner)
|> (\l -> Random.step l puzzle.seed)
tongues =
edgesLongInner
|> List.map2 flip flips
|> List.map (fitWiggly baseWiggly)
|> List.map drawWiggly
border =
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
[ g [] tongues
, g [] (drawingShort "red")
, g [] drawingLong
, g [] markers
]
else
[ g [] tongues
, g [] (drawingShort "black")
, border
]
isOnBorder edge =
let
{ x, y } =
LineSegment2d.midpoint edge
|> Point2d.toUnitless
( mx, my ) =
( round x, round y )
in
mx == 0 || my == 0 || mx == params.width || my == params.height
-- GRID GENERATION HELPERS
rectangular : Int -> Int -> List ( Int, Int )
rectangular nx ny =
let
pointCoordsx =
List.range 0 (nx - 1)
|> List.map (\n -> n * 40 + 20)
pointCoordsy =
List.range 0 (ny - 1)
|> List.map (\n -> n * 40 + 20)
in
List.lift2 Tuple.pair pointCoordsx pointCoordsy
perturbedRectangular : Int -> Int -> Int -> List ( Int, Int )
perturbedRectangular nx ny pert =
let
grid =
rectangular nx ny
intGen =
Random.int -pert pert
( randomCoordList, _ ) =
Random.pair intGen intGen
|> Random.list (List.length grid)
|> (\l -> Random.step l puzzle.seed)
in
List.map2 (\( cx, cy ) ( p1, p2 ) -> ( cx + p1, cy + p2 ))
grid
randomCoordList
randomGrid : Int -> Int -> Int -> List ( Int, Int )
randomGrid xmax ymax npoints =
Random.pair (Random.int 0 xmax) (Random.int 0 ymax)
|> Random.list npoints
|> (\l -> Random.step l puzzle.seed)
|> Tuple.first
-- SVG HELPERS
marker : ( Int, Int ) -> Svg msg
marker ( xc, yc ) =
circle
[ cx <| String.fromInt xc
, cy <| String.fromInt yc
, r "2"
, stroke "#666666"
, fillOpacity "0"
]
[]
-- interlocker : LineSegment2d Unitless coordinates -> CubicSpline2d Unitless coordinates
-- interlocker segment =
-- let
-- ( p1, p2 ) =
-- LineSegment2d.endpoints segment
-- in
-- wiggly
type alias Wiggly =
( CubicSpline2d Unitless (), CubicSpline2d Unitless () )
baseWiggly : Wiggly
baseWiggly =
let
baseShape =
CubicSpline2d.fromControlPoints
-- startpoint
(Point2d.unitless 50 120)
-- control points
(Point2d.unitless 200 120)
(Point2d.unitless 60 70)
-- endpoint
(Point2d.unitless 150 70)
mirroredBaseShape =
CubicSpline2d.mirrorAcross Axis2d.y baseShape
|> CubicSpline2d.translateBy (Vector2d.unitless 300 0)
in
( baseShape, mirroredBaseShape )
fitWiggly : Wiggly -> LineSegment2d Unitless () -> Wiggly
fitWiggly ( w1, w2 ) segment =
let
pivot =
CubicSpline2d.startPoint w1
segmentLen =
LineSegment2d.length segment |> Quantity.toFloat
scale spline =
CubicSpline2d.scaleAbout pivot (1 / 200 * segmentLen) spline
translationVector =
Vector2d.from pivot (LineSegment2d.startPoint segment)
rotationAngle =
LineSegment2d.vector segment
|> Vector2d.direction
|> Maybe.withDefault (Direction2d.radians 0)
|> Direction2d.toAngle
fit w =
scale w
|> CubicSpline2d.translateBy translationVector
|> CubicSpline2d.rotateAround (LineSegment2d.startPoint segment) rotationAngle
in
( fit w1
, fit w2
)
flip : Bool -> LineSegment2d Unitless () -> LineSegment2d Unitless ()
flip flag segment =
if flag then
segment
else
let
pivot =
LineSegment2d.midpoint segment
angle =
Angle.degrees 180
in
LineSegment2d.rotateAround pivot angle segment
drawWiggly : Wiggly -> Svg msg
drawWiggly ( w1, w2 ) =
let
drawHalf spline =
Geometry.Svg.cubicSpline2d [ stroke "black", fillOpacity "0" ] spline
in
g [] [ drawHalf w1, drawHalf w2 ]
-- CANVAS HELPERS
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)
-- border =
-- rect
-- [ x "0", y "0", width wStr, height hStr, fillOpacity "0" ]
-- []
maybeTiles =
if puzzle.draftMode then
[ g [] tiles ]
else
[]
in
svg
[ width wStr
, height hStr
, viewBox <| "0 0 " ++ wStr ++ " " ++ hStr
]
(maybeTiles ++ [ g [] children ])
tile : Int -> Int -> Int -> Svg msg
tile size xc yc =
let
col =
if modBy 2 (xc + yc) == 0 then
"#eeeeee"
else
"#ffffff"
in
rect
[ x (String.fromInt (xc * size))
, y (String.fromInt (yc * size))
, width (String.fromInt size)
, height (String.fromInt size)
, fill col
]
[]