1 | export class Vector3d {
|
2 | constructor(xOrCoords, y, z) {
|
3 | if (typeof xOrCoords !== "number" && xOrCoords) {
|
4 | this.x = xOrCoords.x;
|
5 | this.y = xOrCoords.y;
|
6 | const coords3d = xOrCoords;
|
7 | this.z = coords3d.z ? coords3d.z : 0;
|
8 | }
|
9 | else if (xOrCoords !== undefined && y !== undefined) {
|
10 | this.x = xOrCoords;
|
11 | this.y = y;
|
12 | this.z = z !== null && z !== void 0 ? z : 0;
|
13 | }
|
14 | else {
|
15 | throw new Error("tsParticles - Vector3d not initialized correctly");
|
16 | }
|
17 | }
|
18 | static get origin() {
|
19 | return Vector3d.create(0, 0, 0);
|
20 | }
|
21 | get angle() {
|
22 | return Math.atan2(this.y, this.x);
|
23 | }
|
24 | set angle(angle) {
|
25 | this.updateFromAngle(angle, this.length);
|
26 | }
|
27 | get length() {
|
28 | return Math.sqrt(this.getLengthSq());
|
29 | }
|
30 | set length(length) {
|
31 | this.updateFromAngle(this.angle, length);
|
32 | }
|
33 | static clone(source) {
|
34 | return Vector3d.create(source.x, source.y, source.z);
|
35 | }
|
36 | static create(x, y, z) {
|
37 | return new Vector3d(x, y, z);
|
38 | }
|
39 | add(v) {
|
40 | return Vector3d.create(this.x + v.x, this.y + v.y, this.z + v.z);
|
41 | }
|
42 | addTo(v) {
|
43 | this.x += v.x;
|
44 | this.y += v.y;
|
45 | this.z += v.z;
|
46 | }
|
47 | copy() {
|
48 | return Vector3d.clone(this);
|
49 | }
|
50 | distanceTo(v) {
|
51 | return this.sub(v).length;
|
52 | }
|
53 | distanceToSq(v) {
|
54 | return this.sub(v).getLengthSq();
|
55 | }
|
56 | div(n) {
|
57 | return Vector3d.create(this.x / n, this.y / n, this.z / n);
|
58 | }
|
59 | divTo(n) {
|
60 | this.x /= n;
|
61 | this.y /= n;
|
62 | this.z /= n;
|
63 | }
|
64 | getLengthSq() {
|
65 | return this.x ** 2 + this.y ** 2;
|
66 | }
|
67 | mult(n) {
|
68 | return Vector3d.create(this.x * n, this.y * n, this.z * n);
|
69 | }
|
70 | multTo(n) {
|
71 | this.x *= n;
|
72 | this.y *= n;
|
73 | this.z *= n;
|
74 | }
|
75 | rotate(angle) {
|
76 | return Vector3d.create(this.x * Math.cos(angle) - this.y * Math.sin(angle), this.x * Math.sin(angle) + this.y * Math.cos(angle), 0);
|
77 | }
|
78 | setTo(c) {
|
79 | this.x = c.x;
|
80 | this.y = c.y;
|
81 | const v3d = c;
|
82 | this.z = v3d.z ? v3d.z : 0;
|
83 | }
|
84 | sub(v) {
|
85 | return Vector3d.create(this.x - v.x, this.y - v.y, this.z - v.z);
|
86 | }
|
87 | subFrom(v) {
|
88 | this.x -= v.x;
|
89 | this.y -= v.y;
|
90 | this.z -= v.z;
|
91 | }
|
92 | updateFromAngle(angle, length) {
|
93 | this.x = Math.cos(angle) * length;
|
94 | this.y = Math.sin(angle) * length;
|
95 | }
|
96 | }
|