-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplex.js
More file actions
51 lines (41 loc) · 1.05 KB
/
Complex.js
File metadata and controls
51 lines (41 loc) · 1.05 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
class Complex {
#a; #b
/**
* @param {Number} a
* @param {Number} b
*/
constructor(a=0, b=0) {
this.#a = a;
this.#b = b;
}
get a() { return this.#a; }
get b() { return this.#b; }
get r() {
return Math.sqrt(this.#a*this.#a + this.#b*this.#b);
}
get theta() {
return Math.atan2(this.#b, this.#a);
}
toVector(x0, y0, name) {
return new Arrow(x0, y0, this.r, this.theta, name);
}
add(number) {
switch (number.constructor) {
case Complex:
this.#a = +(this.#a + number.a).toFixed(10);
this.#b = +(this.#b + number.b).toFixed(10);
break;
case Number:
this.#a = +(this.#a + number).toFixed(10);
break;
default:
throw Error("number must be Complex or Number.")
}
return this;
}
static sum(...numbers) {
return numbers.reduce(
(prev, curr) => prev.add(curr),
new Complex(0, 0));
}
}