Przeglądaj źródła

Introduce Vector2

customisations
alemart 10 miesięcy temu
rodzic
commit
2838df7d07
3 zmienionych plików z 198 dodań i 0 usunięć
  1. 55
    0
      docs/api/vector2.md
  2. 1
    0
      mkdocs.yml
  3. 142
    0
      src/geometry/vector2.ts

+ 55
- 0
docs/api/vector2.md Wyświetl plik

@@ -0,0 +1,55 @@
1
+# Vector2
2
+
3
+A vector in 2D space.
4
+
5
+*Since:* 0.4.0
6
+
7
+## Properties
8
+
9
+### x
10
+
11
+`vector.x: number`
12
+
13
+The x coordinate of the vector.
14
+
15
+### y
16
+
17
+`vector.y: number`
18
+
19
+The y coordinate of the vector.
20
+
21
+## Methods
22
+
23
+### length
24
+
25
+`vector.length(): number`
26
+
27
+Compute the length of the vector: `sqrt(x^2 + y^2)`.
28
+
29
+**Returns**
30
+
31
+The length of the vector.
32
+
33
+### equals
34
+
35
+`vector.equals(v: Vector2): boolean`
36
+
37
+Check if `this` and `v` have the same coordinates.
38
+
39
+**Arguments**
40
+
41
+* `v: Vector2`. A vector.
42
+
43
+**Returns**
44
+
45
+`true` if `this` and `v` have the same coordinates.
46
+
47
+### toString
48
+
49
+`vector.toString(): string`
50
+
51
+Generate a string representation of the vector.
52
+
53
+**Returns**
54
+
55
+A string representation of the vector.

+ 1
- 0
mkdocs.yml Wyświetl plik

@@ -97,6 +97,7 @@ nav:
97 97
       - 'Transform': 'api/transform.md'
98 98
       - 'Quaternion': 'api/quaternion.md'
99 99
       - 'Vector3': 'api/vector3.md'
100
+      - 'Vector2': 'api/vector2.md'
100 101
     - 'Visualization':
101 102
       - 'Viewport': 'api/viewport.md'
102 103
       - 'HUD': 'api/hud.md'

+ 142
- 0
src/geometry/vector2.ts Wyświetl plik

@@ -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
+}

Ładowanie…
Anuluj
Zapisz