|
@@ -0,0 +1,142 @@
|
|
1
|
+/*
|
|
2
|
+ * encantar.js
|
|
3
|
+ * GPU-accelerated Augmented Reality for the web
|
|
4
|
+ * Copyright (C) 2022-2024 Alexandre Martins <alemartf(at)gmail.com>
|
|
5
|
+ *
|
|
6
|
+ * This program is free software: you can redistribute it and/or modify
|
|
7
|
+ * it under the terms of the GNU Lesser General Public License as published
|
|
8
|
+ * by the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+ * (at your option) any later version.
|
|
10
|
+ *
|
|
11
|
+ * This program is distributed in the hope that it will be useful,
|
|
12
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+ * GNU Lesser General Public License for more details.
|
|
15
|
+ *
|
|
16
|
+ * You should have received a copy of the GNU Lesser General Public License
|
|
17
|
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
18
|
+ *
|
|
19
|
+ * vector2.ts
|
|
20
|
+ * 2D vectors
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+/** Small number */
|
|
24
|
+const EPSILON = 1e-6;
|
|
25
|
+
|
|
26
|
+// public / non-internal methods do not change the contents of the vector
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+/**
|
|
31
|
+ * A vector in 2D space
|
|
32
|
+ */
|
|
33
|
+export class Vector2
|
|
34
|
+{
|
|
35
|
+ /** x coordinate */
|
|
36
|
+ public x: number;
|
|
37
|
+
|
|
38
|
+ /** y coordinate */
|
|
39
|
+ public y: number;
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+ /**
|
|
45
|
+ * Constructor
|
|
46
|
+ */
|
|
47
|
+ constructor(x: number = 0, y: number = 0)
|
|
48
|
+ {
|
|
49
|
+ this.x = +x;
|
|
50
|
+ this.y = +y;
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ /**
|
|
54
|
+ * Instantiate a zero vector
|
|
55
|
+ * @returns a new zero vector
|
|
56
|
+ */
|
|
57
|
+ static Zero(): Vector2
|
|
58
|
+ {
|
|
59
|
+ return new Vector2(0, 0);
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+ /**
|
|
63
|
+ * The length of this vector
|
|
64
|
+ * @returns sqrt(x^2 + y^2)
|
|
65
|
+ */
|
|
66
|
+ length(): number
|
|
67
|
+ {
|
|
68
|
+ const x = this.x;
|
|
69
|
+ const y = this.y;
|
|
70
|
+
|
|
71
|
+ return Math.sqrt(x*x + y*y);
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ /**
|
|
75
|
+ * Check if this and v have the same coordinates
|
|
76
|
+ * @param v a vector
|
|
77
|
+ * @returns true if this and v have the same coordinates
|
|
78
|
+ */
|
|
79
|
+ equals(v: Vector2): boolean
|
|
80
|
+ {
|
|
81
|
+ return this.x === v.x && this.y === v.y;
|
|
82
|
+ }
|
|
83
|
+
|
|
84
|
+ /**
|
|
85
|
+ * Convert to string
|
|
86
|
+ * @returns a string
|
|
87
|
+ */
|
|
88
|
+ toString(): string
|
|
89
|
+ {
|
|
90
|
+ const x = this.x.toFixed(5);
|
|
91
|
+ const y = this.y.toFixed(5);
|
|
92
|
+
|
|
93
|
+ return `Vector2(${x},${y})`;
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ /**
|
|
97
|
+ * Normalize this vector
|
|
98
|
+ * @returns this vector, normalized
|
|
99
|
+ * @internal
|
|
100
|
+ */
|
|
101
|
+ _normalize(): Vector2
|
|
102
|
+ {
|
|
103
|
+ const length = this.length();
|
|
104
|
+
|
|
105
|
+ if(length < EPSILON) // zero?
|
|
106
|
+ return this;
|
|
107
|
+
|
|
108
|
+ this.x /= length;
|
|
109
|
+ this.y /= length;
|
|
110
|
+
|
|
111
|
+ return this;
|
|
112
|
+ }
|
|
113
|
+
|
|
114
|
+ /**
|
|
115
|
+ * Copy v to this
|
|
116
|
+ * @param v a vector
|
|
117
|
+ * @returns this vector
|
|
118
|
+ * @internal
|
|
119
|
+ */
|
|
120
|
+ _copyFrom(v: Vector2): Vector2
|
|
121
|
+ {
|
|
122
|
+ this.x = v.x;
|
|
123
|
+ this.y = v.y;
|
|
124
|
+
|
|
125
|
+ return this;
|
|
126
|
+ }
|
|
127
|
+
|
|
128
|
+ /**
|
|
129
|
+ * Set the coordinates of this vector
|
|
130
|
+ * @param x x-coordinate
|
|
131
|
+ * @param y y-coordinate
|
|
132
|
+ * @returns this vector
|
|
133
|
+ * @internal
|
|
134
|
+ */
|
|
135
|
+ _set(x: number, y: number): Vector2
|
|
136
|
+ {
|
|
137
|
+ this.x = +x;
|
|
138
|
+ this.y = +y;
|
|
139
|
+
|
|
140
|
+ return this;
|
|
141
|
+ }
|
|
142
|
+}
|