-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathMain.elm
290 lines (224 loc) · 6.12 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
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
module Main (..) where
{- Oh fudge, you're looking at the source code. -}
import Color exposing (Color)
import Graphics.Collage exposing (..)
import Graphics.Element exposing (Element, flow, down, container, centered, middle)
import Mouse
import Text exposing (link, fromString)
import Window
------------------------------------------------------------
-- Types
------------------------------------------------------------
type alias Walls =
List Line
type alias Line =
{ position : Position
, vector : Vector
}
type alias Position =
{ x : Float
, y : Float
}
type alias Vector =
{ length : Float
, angle : Float
}
start : Line -> Position
start line =
line.position
end : Line -> Position
end line =
let
( dx, dy ) =
fromPolar ( line.vector.length, line.vector.angle )
in
{ x = (line.position.x + dx)
, y = (line.position.y + dy)
}
toXY : Position -> ( Float, Float )
toXY p =
( p.x, p.y )
withLength : Float -> Line -> Line
withLength length line =
let
vector =
line.vector
in
{ line | vector = { vector | length = length } }
adjustAngle : Float -> Line -> Line
adjustAngle delta line =
let
vector =
line.vector
in
{ line | vector = { vector | angle = vector.angle + delta } }
lineBetween : Position -> Position -> Line
lineBetween from to =
{ position = from
, vector = vectorBetween from to
}
vectorBetween : Position -> Position -> Vector
vectorBetween p1 p2 =
let
dx =
p2.x - p1.x
dy =
p2.y - p1.y
in
{ length = sqrt (dx * dx + dy * dy)
, angle = atan2 (p2.y - p1.y) (p2.x - p1.x)
}
solveRays : Walls -> Position -> List Line
solveRays walls rayStart =
walls
|> List.concatMap (toRays rayStart)
|> List.filterMap (curtail walls)
curtail : Walls -> Line -> Maybe Line
curtail walls line =
walls
|> List.filterMap (intersect line)
|> List.sortBy (.vector >> .length)
|> List.head
norms : Line -> ( Float, Float )
norms line =
( cos line.vector.angle
, sin line.vector.angle
)
intersect : Line -> Line -> Maybe Line
intersect r s =
let
( r_px, r_py ) =
toXY (start r)
( s_px, s_py ) =
toXY (start s)
( r_dx, r_dy ) =
norms r
( s_dx, s_dy ) =
norms s
sm =
((r_px * r_dy) - (r_py * r_dx) + (s_py * r_dx) - (s_px * r_dy))
/ ((s_dx * r_dy) - (s_dy * r_dx))
rm =
((s_px - r_px + (s_dx * sm)) / r_dx)
in
if isNaN sm || isNaN rm then
Nothing
else if sm < 0 then
Nothing
else if s.vector.length < sm then
Nothing
else if rm < 0 then
Nothing
else
Just (withLength rm r)
toRays : Position -> Line -> List Line
toRays position line =
let
rayToStart =
lineBetween position (start line)
rayToEnd =
lineBetween position (end line)
in
[ adjustAngle (degrees 0.5) rayToStart
, adjustAngle (degrees -0.5) rayToStart
, adjustAngle (degrees 0.5) rayToEnd
, adjustAngle (degrees -0.5) rayToEnd
]
------------------------------------------------------------
-- View
------------------------------------------------------------
view : Walls -> ( Int, Int ) -> ( Int, Int ) -> Element
view walls ( w', h' ) ( x', y' ) =
let
( w, h ) =
( toFloat w', toFloat h' )
rayPosition =
{ x = toFloat x' - (w / 2)
, y = (h / 2) - toFloat y'
}
in
flow
down
[ collage
w'
h'
[ group
(let
solutions =
solveRays walls rayPosition
|> List.sortBy (.vector >> .angle)
cycled =
solutions ++ (List.take 1 solutions)
in
List.map2 (,) cycled (List.tail cycled |> Maybe.withDefault [])
|> List.map (drawTriangles rayColor)
)
, circle 5
|> filled Color.red
|> move (toXY rayPosition)
, group (List.map (drawLine wallLineStyle) walls)
]
, [ fromString "A raycasting hack in "
, link "http://elm-lang.org/" (fromString "Elm")
, fromString ", based on "
, link "http://ncase.me/sight-and-light" (fromString "this excellent tutorial")
, fromString "."
]
|> Text.concat
|> centered
|> container w' 30 middle
, link "https://github.com/krisajenkins/elm-rays" (fromString "Source Code")
|> centered
|> container w' 30 middle
]
drawLine : LineStyle -> Line -> Form
drawLine lineStyle line =
let
lineStart =
toXY (start line)
lineEnd =
toXY (end line)
in
segment lineStart lineEnd
|> traced lineStyle
drawTriangles : Color -> ( Line, Line ) -> Form
drawTriangles color ( a, b ) =
[ start a
, end a
, end b
, start b
]
|> List.map toXY
|> polygon
|> filled color
------------------------------------------------------------
-- State
------------------------------------------------------------
initialWalls : Walls
initialWalls =
[ { position = { x = -300, y = -300 }, vector = { length = 600, angle = degrees 0 } }
, { position = { x = 300, y = -300 }, vector = { length = 600, angle = degrees 90 } }
, { position = { x = -300, y = -300 }, vector = { length = 600, angle = degrees 90 } }
, { position = { x = 300, y = 300 }, vector = { length = 600, angle = degrees 180 } }
, { position = { x = 100, y = 100 }, vector = { length = 50, angle = degrees 315 } }
, { position = { x = -80, y = 100 }, vector = { length = 50, angle = degrees 290 } }
, { position = { x = -200, y = 180 }, vector = { length = 150, angle = degrees 250 } }
, { position = { x = 150, y = -100 }, vector = { length = 120, angle = degrees 235 } }
, { position = { x = -230, y = -250 }, vector = { length = 300, angle = degrees 70 } }
, { position = { x = 0, y = -150 }, vector = { length = 300, angle = degrees 30 } }
]
rayColor : Color
rayColor =
Color.lightYellow
wallLineStyle : LineStyle
wallLineStyle =
{ defaultLine
| width = 8
, cap = Round
}
main : Signal Element
main =
Signal.map2
(view initialWalls)
Window.dimensions
Mouse.position