-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVec2.js
57 lines (48 loc) · 909 Bytes
/
Vec2.js
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
class Vec2
{
constructor(x, y)
{
this.x = x;
this.y = y;
}
add(vec)
{
//component-wise addition
return new Vec2(this.x + vec.x, this.y + vec.y);
}
sub(vec)
{
//component-wise subtraction
return new Vec2(this.x - vec.x, this.y - vec.y);
}
mul(scalar)
{
//multiply vector by scalar
return new Vec2(this.x * scalar, this.y * scalar);
}
dot(vec)
{
return this.x * vec.x + this.y * vec.y;
}
cross(vec)
{
return this.x * vec.y - this.y * vec.x;
}
length()
{
return Math.sqrt(this.dot(this));
}
normalize()
{
return new Vec2(this.x / this.length(), this.y / this.length());
}
perp()
{
return new Vec2(-this.y, this.x);
}
set(vec)
{
this.x = vec.x;
this.y = vec.y;
}
}