浏览代码

Vector2, Vector3: add methods normalized(), plus(), minus() and times()

customisations
alemart 10 个月前
父节点
当前提交
0465beda87
共有 4 个文件被更改,包括 182 次插入0 次删除
  1. 52
    0
      docs/api/vector2.md
  2. 52
    0
      docs/api/vector3.md
  3. 39
    0
      src/geometry/vector2.ts
  4. 39
    0
      src/geometry/vector3.ts

+ 52
- 0
docs/api/vector2.md 查看文件

@@ -95,6 +95,58 @@ Compute a unit vector pointing to `v` from `this`.
95 95
 
96 96
 A new unit vector pointing to `v` from `this`.
97 97
 
98
+### normalized
99
+
100
+`vector.normalized(): Vector2`
101
+
102
+Compute a unit vector with the same direction as `this`.
103
+
104
+**Returns**
105
+
106
+A new unit vector with the same direction as `this`.
107
+
108
+### plus
109
+
110
+`vector.plus(v: Vector2): Vector2`
111
+
112
+Compute the sum between `this` vector and `v`.
113
+
114
+**Arguments**
115
+
116
+* `v: Vector2`. A vector.
117
+
118
+**Returns**
119
+
120
+A new vector equal to the sum between `this` and `v`.
121
+
122
+### minus
123
+
124
+`vector.minus(v: Vector2): Vector2`
125
+
126
+Compute the difference between `this` vector and `v`.
127
+
128
+**Arguments**
129
+
130
+* `v: Vector2`. A vector.
131
+
132
+**Returns**
133
+
134
+A new vector equal to the difference `this` - `v`.
135
+
136
+### times
137
+
138
+`vector.times(scale: number): Vector2`
139
+
140
+Compute `this` vector multiplied by a scale factor.
141
+
142
+**Arguments**
143
+
144
+* `scale: number`. A scale factor.
145
+
146
+**Returns**
147
+
148
+A new vector equal to the multiplication between `this` and the scale factor.
149
+
98 150
 ### equals
99 151
 
100 152
 `vector.equals(v: Vector2): boolean`

+ 52
- 0
docs/api/vector3.md 查看文件

@@ -116,6 +116,58 @@ Compute the cross product of `this` and `v`.
116 116
 
117 117
 The cross product `this` x `v`.
118 118
 
119
+### normalized
120
+
121
+`vector.normalized(): Vector3`
122
+
123
+Compute a unit vector with the same direction as `this`.
124
+
125
+**Returns**
126
+
127
+A new unit vector with the same direction as `this`.
128
+
129
+### plus
130
+
131
+`vector.plus(v: Vector3): Vector3`
132
+
133
+Compute the sum between `this` vector and `v`.
134
+
135
+**Arguments**
136
+
137
+* `v: Vector3`. A vector.
138
+
139
+**Returns**
140
+
141
+A new vector equal to the sum between `this` and `v`.
142
+
143
+### minus
144
+
145
+`vector.minus(v: Vector3): Vector3`
146
+
147
+Compute the difference between `this` vector and `v`.
148
+
149
+**Arguments**
150
+
151
+* `v: Vector3`. A vector.
152
+
153
+**Returns**
154
+
155
+A new vector equal to the difference `this` - `v`.
156
+
157
+### times
158
+
159
+`vector.times(scale: number): Vector3`
160
+
161
+Compute `this` vector multiplied by a scale factor.
162
+
163
+**Arguments**
164
+
165
+* `scale: number`. A scale factor.
166
+
167
+**Returns**
168
+
169
+A new vector equal to the multiplication between `this` and the scale factor.
170
+
119 171
 ### equals
120 172
 
121 173
 `vector.equals(v: Vector3): boolean`

+ 39
- 0
src/geometry/vector2.ts 查看文件

@@ -136,6 +136,45 @@ export class Vector2
136 136
     }
137 137
 
138 138
     /**
139
+     * Compute a unit vector with the same direction as this
140
+     * @returns a new unit vector with the same direction as this
141
+     */
142
+    normalized(): Vector2
143
+    {
144
+        return this._clone()._normalize();
145
+    }
146
+
147
+    /**
148
+     * Compute the sum between this vector and v
149
+     * @param v a vector
150
+     * @returns a new vector equal to the sum between this and v
151
+     */
152
+    plus(v: Vector2): Vector2
153
+    {
154
+        return this._clone()._add(v);
155
+    }
156
+
157
+    /**
158
+     * Compute the difference between this vector and v
159
+     * @param v a vector
160
+     * @returns a new vector equal to the difference this - v
161
+     */
162
+    minus(v: Vector2): Vector2
163
+    {
164
+        return this._clone()._subtract(v);
165
+    }
166
+
167
+    /**
168
+     * Compute the multiplication between this vector and a scale factor
169
+     * @param scale scalar quantity
170
+     * @returns a new vector equal to the multiplication between this and the scale factor
171
+     */
172
+    times(scale: number): Vector2
173
+    {
174
+        return this._clone()._scale(scale);
175
+    }
176
+
177
+    /**
139 178
      * Check if this and v have the same coordinates
140 179
      * @param v a vector
141 180
      * @returns true if this and v have the same coordinates

+ 39
- 0
src/geometry/vector3.ts 查看文件

@@ -163,6 +163,45 @@ export class Vector3
163 163
     }
164 164
 
165 165
     /**
166
+     * Compute a unit vector with the same direction as this
167
+     * @returns a new unit vector with the same direction as this
168
+     */
169
+    normalized(): Vector3
170
+    {
171
+        return this._clone()._normalize();
172
+    }
173
+
174
+    /**
175
+     * Compute the sum between this vector and v
176
+     * @param v a vector
177
+     * @returns a new vector equal to the sum between this and v
178
+     */
179
+    plus(v: Vector3): Vector3
180
+    {
181
+        return this._clone()._add(v);
182
+    }
183
+
184
+    /**
185
+     * Compute the difference between this vector and v
186
+     * @param v a vector
187
+     * @returns a new vector equal to the difference this - v
188
+     */
189
+    minus(v: Vector3): Vector3
190
+    {
191
+        return this._clone()._subtract(v);
192
+    }
193
+
194
+    /**
195
+     * Compute the multiplication between this vector and a scale factor
196
+     * @param scale scalar quantity
197
+     * @returns a new vector equal to the multiplication between this and the scale factor
198
+     */
199
+    times(scale: number): Vector3
200
+    {
201
+        return this._clone()._scale(scale);
202
+    }
203
+
204
+    /**
166 205
      * Check if this and v have the same coordinates
167 206
      * @param v a vector
168 207
      * @returns true if this and v have the same coordinates

正在加载...
取消
保存