-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.ts
More file actions
92 lines (81 loc) · 1.97 KB
/
Vector.ts
File metadata and controls
92 lines (81 loc) · 1.97 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
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
/*
A Vector type with some basic math functionality defined.
*/
export class Vector {
/*
Construct a Vector with an X component and a Y component
*/
constructor(private readonly x:number, private readonly y:number) { }
/*
Get the direction of the Vector.
*/
public getAngle() {
return Math.atan(this.y / this.x)
}
/*
Get the magnitude of the Vector.
*/
public getMagnitude() {
return Math.sqrt(Math.pow(this.y,2) + Math.pow(this.x,2))
}
/*
Get the X component of the Vector.
*/
public getX() {
return this.x;
}
/*
Set the X component of the Vector.
*/
public setX = (x: number) : Vector =>{
return new Vector(x, this.y);
}
/*
Add a scalar to the X component of the Vector.
*/
public plusX = (x: number) : Vector =>{
return new Vector(this.x+x, this.y);
}
/*
Round X to the nearest 10th.
*/
public roundX = () : Vector => {
return new Vector(Math.round(this.x*10)/10,this.y);
}
/*
Get the Y component of the Vector.
*/
public getY() {
return this.y;
}
/*
Set the Y component of the Vector.
*/
public setY = (y: number) : Vector =>{
return new Vector(this.x, y);
}
/*
Add a scalar to the Y component of the Vector.
*/
public plusY = (y: number) : Vector =>{
return new Vector(this.x, this.y+y);
}
/*
Combine two Vectors using addition.
*/
public plusVec = (vec: Vector) : Vector =>{
return new Vector(this.x+vec.x, this.y + vec.y);
}
/*
Multiply the Vector by a scalar.
*/
public mult = (scal: number) : Vector =>{
return new Vector(this.x*scal, this.y*scal);
}
/*
Divide the Vector by a scalar.
*/
public div = (scal: number) : Vector =>{
return new Vector(this.x/scal, this.y/scal);
}
}