-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.elm
192 lines (150 loc) · 3.81 KB
/
Main.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
module Main exposing (main)
import Dict exposing (Dict)
import List.Extra as List
import Svg exposing (Svg)
import Svg.Attributes exposing (..)
puzzle =
{ draftMode = True
, piecesX = 12
, piecesY = 6
, pixelsPerCell = 50
}
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
}
main : Svg msg
main =
let
grid =
rectangularGrid puzzle.piecesX puzzle.piecesY
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
]
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
-- 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"
]
[]
-- 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
]
[]