-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVec2.elm
61 lines (46 loc) · 884 Bytes
/
Vec2.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
module Vec2 exposing
( Vec2
, add
, subtract
, scale
, dot
, length
, normalize
, clamp
)
type alias Vec2 =
{ x : Float
, y : Float
}
add : Vec2 -> Vec2 -> Vec2
add v1 v2 =
{ x = v1.x + v2.x
, y = v1.y + v2.y
}
subtract : Vec2 -> Vec2 -> Vec2
subtract v1 v2 =
v1 `add` (scale -1 v2)
scale : number -> Vec2 -> Vec2
scale scalar vector =
{ x = scalar * vector.x
, y = scalar * vector.y
}
dot : Vec2 -> Vec2 -> Float
dot v1 v2 =
v1.x * v2.x + v1.y * v2.y
clamp : Vec2 -> Vec2 -> Vec2 -> Vec2
clamp min max vec =
{ x = Basics.clamp min.x max.x vec.x
, y = Basics.clamp min.y max.y vec.y
}
length : Vec2 -> Float
length v =
sqrt (v.x ^ 2 + v.y ^ 2)
normalize : Vec2 -> Vec2
normalize v =
let
l = length v
in
{ x = v.x / l
, y = v.y / l
}