-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector2.js
More file actions
49 lines (39 loc) · 1.04 KB
/
Vector2.js
File metadata and controls
49 lines (39 loc) · 1.04 KB
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
// Please carefully review the rules about academic integrity found in the academicIntegrity.md file found at the root of this project.
/**
* Represents a 2D vector (direction) or 2D position
*
* See https://docs.unity3d.com/6000.1/Documentation/ScriptReference/Vector2.html
*/
class Vector2 {
constructor(x, y) {
this.x = x
this.y = y
}
x
y
add(other){
return new Vector2(this.x+other.x, this.y+other.y)
}
minus(other){
return new Vector2(this.x - other.x, this.y - other.y)
}
orthogonal(){
return new Vector2(-this.y, this.x)
}
dot(other){
return this.x*other.x+this.y*other.y
}
times(scalar){
return new Vector2(this.x * scalar, this.y*scalar)
}
get magnitude(){
return Math.sqrt(this.x**2+this.y**2)
}
normalized(){
if(this.magnitude == 0) return new Vector2(0,0)
return new Vector2(this.x/this.magnitude, this.y/this.magnitude)
}
clone(){
return new Vector2(this.x, this.y)
}
}