Ver código fonte

Add methods _add(), _subtract() and _scale() to Vector2 and Vector3

customisations
alemart 10 meses atrás
pai
commit
6c5d792b1e
2 arquivos alterados com 110 adições e 23 exclusões
  1. 52
    10
      src/geometry/vector2.ts
  2. 58
    13
      src/geometry/vector3.ts

+ 52
- 10
src/geometry/vector2.ts Ver arquivo

@@ -94,6 +94,35 @@ export class Vector2
94 94
     }
95 95
 
96 96
     /**
97
+     * Set the coordinates of this vector
98
+     * @param x x-coordinate
99
+     * @param y y-coordinate
100
+     * @returns this vector
101
+     * @internal
102
+     */
103
+    _set(x: number, y: number): Vector2
104
+    {
105
+        this.x = +x;
106
+        this.y = +y;
107
+
108
+        return this;
109
+    }
110
+
111
+    /**
112
+     * Copy v to this vector
113
+     * @param v a vector
114
+     * @returns this vector
115
+     * @internal
116
+     */
117
+    _copyFrom(v: Vector2): Vector2
118
+    {
119
+        this.x = v.x;
120
+        this.y = v.y;
121
+
122
+        return this;
123
+    }
124
+
125
+    /**
97 126
      * Normalize this vector
98 127
      * @returns this vector, normalized
99 128
      * @internal
@@ -112,30 +141,43 @@ export class Vector2
112 141
     }
113 142
 
114 143
     /**
115
-     * Copy v to this
144
+     * Add v to this vector
116 145
      * @param v a vector
117 146
      * @returns this vector
118 147
      * @internal
119 148
      */
120
-    _copyFrom(v: Vector2): Vector2
149
+    _add(v: Vector2): Vector2
121 150
     {
122
-        this.x = v.x;
123
-        this.y = v.y;
151
+        this.x += v.x;
152
+        this.y += v.y;
124 153
 
125 154
         return this;
126 155
     }
127 156
 
128 157
     /**
129
-     * Set the coordinates of this vector
130
-     * @param x x-coordinate
131
-     * @param y y-coordinate
158
+     * Subtract v from this vector
159
+     * @param v a vector
132 160
      * @returns this vector
133 161
      * @internal
134 162
      */
135
-    _set(x: number, y: number): Vector2
163
+    _subtract(v: Vector2): Vector2
136 164
     {
137
-        this.x = +x;
138
-        this.y = +y;
165
+        this.x -= v.x;
166
+        this.y -= v.y;
167
+
168
+        return this;
169
+    }
170
+
171
+    /**
172
+     * Scale this vector by a scalar
173
+     * @param s scalar
174
+     * @returns this vector
175
+     * @internal
176
+     */
177
+    _scale(s: number): Vector2
178
+    {
179
+        this.x *= s;
180
+        this.y *= s;
139 181
 
140 182
         return this;
141 183
     }

+ 58
- 13
src/geometry/vector3.ts Ver arquivo

@@ -99,6 +99,38 @@ export class Vector3
99 99
     }
100 100
 
101 101
     /**
102
+     * Set the coordinates of this vector
103
+     * @param x x-coordinate
104
+     * @param y y-coordinate
105
+     * @param z z-coordinate
106
+     * @returns this vector
107
+     * @internal
108
+     */
109
+    _set(x: number, y: number, z: number): Vector3
110
+    {
111
+        this.x = +x;
112
+        this.y = +y;
113
+        this.z = +z;
114
+
115
+        return this;
116
+    }
117
+
118
+    /**
119
+     * Copy v to this
120
+     * @param v a vector
121
+     * @returns this vector
122
+     * @internal
123
+     */
124
+    _copyFrom(v: Vector3): Vector3
125
+    {
126
+        this.x = v.x;
127
+        this.y = v.y;
128
+        this.z = v.z;
129
+
130
+        return this;
131
+    }
132
+
133
+    /**
102 134
      * Normalize this vector
103 135
      * @returns this vector, normalized
104 136
      * @internal
@@ -118,33 +150,46 @@ export class Vector3
118 150
     }
119 151
 
120 152
     /**
121
-     * Copy v to this
153
+     * Add v to this vector
122 154
      * @param v a vector
123 155
      * @returns this vector
124 156
      * @internal
125 157
      */
126
-    _copyFrom(v: Vector3): Vector3
158
+    _add(v: Vector3): Vector3
127 159
     {
128
-        this.x = v.x;
129
-        this.y = v.y;
130
-        this.z = v.z;
160
+        this.x += v.x;
161
+        this.y += v.y;
162
+        this.z += v.z;
131 163
 
132 164
         return this;
133 165
     }
134 166
 
135 167
     /**
136
-     * Set the coordinates of this vector
137
-     * @param x x-coordinate
138
-     * @param y y-coordinate
139
-     * @param z z-coordinate
168
+     * Subtract v from this vector
169
+     * @param v a vector
140 170
      * @returns this vector
141 171
      * @internal
142 172
      */
143
-    _set(x: number, y: number, z: number): Vector3
173
+    _subtract(v: Vector3): Vector3
144 174
     {
145
-        this.x = +x;
146
-        this.y = +y;
147
-        this.z = +z;
175
+        this.x -= v.x;
176
+        this.y -= v.y;
177
+        this.z -= v.z;
178
+
179
+        return this;
180
+    }
181
+
182
+    /**
183
+     * Scale this vector by a scalar
184
+     * @param s scalar
185
+     * @returns this vector
186
+     * @internal
187
+     */
188
+    _scale(s: number): Vector3
189
+    {
190
+        this.x *= s;
191
+        this.y *= s;
192
+        this.z *= s;
148 193
 
149 194
         return this;
150 195
     }

Carregando…
Cancelar
Salvar