-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.elm
166 lines (123 loc) · 3.95 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
module Main exposing (Model, Msg(..), Point, Segment, canvas, container, init, main, orientationsGenerator, placeSegment, scale, segments, update, view, viewSegment)
import Browser
import Grid exposing (grid)
import Html exposing (Html, text)
import Html.Attributes
import Orientation exposing (Orientation)
import Random exposing (Generator)
import Svg exposing (Svg)
import Svg.Attributes
-- MODEL
type alias Model =
{ size : Int
, orientations : List Orientation
}
type alias Point =
{ x : Float, y : Float }
type alias Segment =
{ start : Point, end : Point }
init : () -> ( Model, Cmd Msg )
init _ =
( { size = 15
, orientations = []
}
, Random.generate OrientationsGenerated (orientationsGenerator (15 * 15))
)
segments : Int -> List Segment
segments n =
List.repeat n { start = Point -0.5 -0.5, end = Point 0.5 0.5 }
-- UPDATE
type Msg
= OrientationsGenerated (List Orientation)
update : Msg -> Model -> ( Model, Cmd Msg )
update message model =
case message of
OrientationsGenerated orientations ->
( { model | orientations = orientations }, Cmd.none )
orientationsGenerator : Int -> Generator (List Orientation)
orientationsGenerator n =
Random.list n Orientation.generator
-- VIEW
view : Model -> Browser.Document msg
view model =
{ title = "Labyrinth"
, body =
[ Html.div [ Html.Attributes.style "padding" "2rem" ]
[ List.map3
placeSegment
(grid model.size)
(segments (model.size * model.size))
model.orientations
|> canvas (toFloat model.size * scale)
, Html.footer
[ Html.Attributes.style "padding" "2rem"
, Html.Attributes.style "text-align" "center"
]
[ Html.a
[ Html.Attributes.href "https://github.com/crackofdusk/labyrinth"
, Html.Attributes.style "font-family" "monospace"
]
[ Html.text "Source code" ]
]
]
|> container
]
}
scale : Float
scale =
50.0
placeSegment : ( Int, Int ) -> Segment -> Orientation -> Svg msg
placeSegment ( x, y ) segment orientation =
Svg.g
[ Svg.Attributes.transform
("scale("
++ String.fromFloat scale
++ ")"
++ " translate("
++ String.fromInt x
++ ","
++ String.fromInt y
++ ")"
++ " rotate("
++ String.fromInt (Orientation.toAngle orientation)
++ ")"
)
, Svg.Attributes.strokeWidth <| String.fromFloat (2 / scale) ++ "px"
]
[ viewSegment segment ]
viewSegment : Segment -> Html msg
viewSegment segment =
Svg.line
[ Svg.Attributes.x1 (String.fromFloat segment.start.x)
, Svg.Attributes.x2 (String.fromFloat segment.end.x)
, Svg.Attributes.y1 (String.fromFloat segment.start.y)
, Svg.Attributes.y2 (String.fromFloat segment.end.y)
, Svg.Attributes.stroke "black"
]
[]
container : Html msg -> Html msg
container element =
Html.div
[ Html.Attributes.style "display" "flex"
, Html.Attributes.style "justify-content" "center"
, Html.Attributes.style "align-items" "center"
, Html.Attributes.style "height" "100vh"
]
[ element ]
canvas : Float -> List (Svg msg) -> Svg msg
canvas size elements =
Svg.svg
[ Svg.Attributes.viewBox ("0 0 " ++ String.fromFloat size ++ " " ++ String.fromFloat size)
, Svg.Attributes.width (String.fromFloat size)
, Svg.Attributes.height (String.fromFloat size)
]
elements
-- MAIN
main : Program () Model Msg
main =
Browser.document
{ init = init
, view = view
, update = update
, subscriptions = \_ -> Sub.none
}