-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector2f.ts
113 lines (100 loc) · 3.12 KB
/
vector2f.ts
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
const process = (
op: (x: number, y: number) => void,
arg1: Vector2f | number,
arg2?: number,
): void => {
if (typeof arg2 === "number") {
op(arg1 as number, arg2);
} else if (typeof arg1 === "number") {
op(arg1, arg1);
} else {
op(arg1.x, arg1.y);
}
};
/**
* Class is used for holding 2 numeric values and manipulation with them
*/
export class Vector2f {
/**
* the X value of vector
*/
public y = 0;
/**
* the Y value of vector
*/
public x = 0;
public constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
/**
* Function set vectors values and return object itself
*
* @param arg1 parameter can by {@link Vector2f} or number representing {@link x} if arg2 is passed otherwise {@link x} and {@link y}
* @param arg2 is {@link y} value for vector
* @returns created {@link Vector2f}
*/
public set(arg1: Vector2f | number, arg2?: number): this {
process((x, y) => {
this.x = x;
this.y = y;
}, arg1, arg2);
return this;
}
/**
* Function add values into current values and return object itself
*
* @param arg1 parameter can by {@link Vector2f} or number representing {@link x} if arg2 is passed otherwise {@link x} and {@link y}
* @param arg2 is {@link y} value for vector
* @returns updated {@link Vector2f}
*/
public add(arg1: Vector2f | number, arg2?: number): this {
process((x, y) => {
this.x += x;
this.y += y;
}, arg1, arg2);
return this;
}
/**
* Function divide current values and return object itself
*
* @param arg1 parameter can by {@link Vector2f} or number representing {@link x} if arg2 is passed otherwise {@link x} and {@link y}
* @param arg2 is {@link y} value for vector
* @returns updated {@link Vector2f}
*/
public div(arg1: Vector2f | number, arg2?: number): this {
process((x, y) => {
this.x /= x;
this.y /= y;
}, arg1, arg2);
return this;
}
/**
* Function multiply current values and return object itself
*
* @param arg1 parameter can by {@link Vector2f} or number representing {@link x} if arg2 is passed otherwise {@link x} and {@link y}
* @param arg2 is {@link y} value for vector
* @returns updated {@link Vector2f}
*/
public mul(arg1: Vector2f | number, arg2?: number): this {
process((x, y) => {
this.x *= x;
this.y *= y;
}, arg1, arg2);
return this;
}
/**
* Function subtract values from current values and return object itself
*
* @param arg1 parameter can by {@link Vector2f} or number representing {@link x} if arg2 is passed otherwise {@link x} and {@link y}
* @param arg2 is {@link y} value for vector
* @returns updated {@link Vector2f}
*/
public sub(arg1: Vector2f | number, arg2?: number): this {
process((x, y) => {
this.x -= x;
this.y -= y;
}, arg1, arg2);
return this;
}
}