q5 2.9.22 → 2.9.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/launch.json +26 -0
- package/bun.lockb +0 -0
- package/p5-tests/js/chai_helpers.js +20 -0
- package/p5-tests/js/mocha_setup.js +2 -0
- package/p5-tests/js/modernizr.js +5 -0
- package/p5-tests/js/p5_helpers.js +135 -0
- package/p5-tests/js/sinon.js +5949 -0
- package/p5-tests/mocha.css +289 -0
- package/p5-tests/test.html +71 -0
- package/p5-tests/unit/color/color_conversion.js +68 -0
- package/p5-tests/unit/color/creating_reading.js +217 -0
- package/p5-tests/unit/color/p5.Color.js +1000 -0
- package/p5-tests/unit/color/setting.js +289 -0
- package/p5-tests/unit/core/2d_primitives.js +490 -0
- package/p5-tests/unit/core/attributes.js +115 -0
- package/p5-tests/unit/core/curves.js +139 -0
- package/p5-tests/unit/core/environment.js +248 -0
- package/p5-tests/unit/core/error_helpers.js +1158 -0
- package/p5-tests/unit/core/main.js +340 -0
- package/p5-tests/unit/core/p5.Element.js +773 -0
- package/p5-tests/unit/core/p5.Graphics.js +179 -0
- package/p5-tests/unit/core/preload.js +285 -0
- package/p5-tests/unit/core/rendering.js +116 -0
- package/p5-tests/unit/core/structure.js +293 -0
- package/p5-tests/unit/core/transform.js +144 -0
- package/p5-tests/unit/core/version.js +28 -0
- package/p5-tests/unit/core/vertex.js +137 -0
- package/p5-tests/unit/dom/dom.js +2146 -0
- package/p5-tests/unit/events/acceleration.js +213 -0
- package/p5-tests/unit/events/keyboard.js +179 -0
- package/p5-tests/unit/events/mouse.js +487 -0
- package/p5-tests/unit/events/touch.js +180 -0
- package/p5-tests/unit/image/downloading.js +379 -0
- package/p5-tests/unit/image/filters.js +92 -0
- package/p5-tests/unit/image/loading.js +413 -0
- package/p5-tests/unit/image/p5.Image.js +201 -0
- package/p5-tests/unit/image/pixels.js +234 -0
- package/p5-tests/unit/io/files.js +378 -0
- package/p5-tests/unit/io/loadBytes.js +149 -0
- package/p5-tests/unit/io/loadImage.js +123 -0
- package/p5-tests/unit/io/loadJSON.js +185 -0
- package/p5-tests/unit/io/loadModel.js +215 -0
- package/p5-tests/unit/io/loadShader.js +176 -0
- package/p5-tests/unit/io/loadStrings.js +140 -0
- package/p5-tests/unit/io/loadTable.js +183 -0
- package/p5-tests/unit/io/loadXML.js +127 -0
- package/p5-tests/unit/io/saveModel.js +113 -0
- package/p5-tests/unit/io/saveTable.js +142 -0
- package/p5-tests/unit/math/calculation.js +452 -0
- package/p5-tests/unit/math/noise.js +66 -0
- package/p5-tests/unit/math/p5.Vector.js +1886 -0
- package/p5-tests/unit/math/random.js +177 -0
- package/p5-tests/unit/math/trigonometry.js +144 -0
- package/p5-tests/unit/spec.js +50 -0
- package/p5-tests/unit/typography/attributes.js +120 -0
- package/p5-tests/unit/typography/loadFont.js +162 -0
- package/p5-tests/unit/typography/p5.Font.js +63 -0
- package/p5-tests/unit/utilities/conversion.js +329 -0
- package/p5-tests/unit/utilities/time_date.js +133 -0
- package/package.json +1 -1
- package/q5.d.ts +172 -55
- package/q5.js +55 -40
- package/q5.min.js +1 -1
- package/src/q5-2d-image.js +7 -3
- package/src/q5-core.js +6 -2
- package/src/q5-math.js +1 -0
- package/src/q5-webgpu-canvas.js +8 -7
- package/src/q5-webgpu-drawing.js +15 -12
- package/src/q5-webgpu-image.js +1 -1
- package/src/q5-webgpu-text.js +17 -15
|
@@ -0,0 +1,1886 @@
|
|
|
1
|
+
suite('p5.Vector', function() {
|
|
2
|
+
var RADIANS = 'radians';
|
|
3
|
+
var DEGREES = 'degrees';
|
|
4
|
+
|
|
5
|
+
var myp5;
|
|
6
|
+
var v;
|
|
7
|
+
|
|
8
|
+
setup(function(done) {
|
|
9
|
+
new p5(function(p) {
|
|
10
|
+
p.setup = function() {
|
|
11
|
+
myp5 = p;
|
|
12
|
+
done();
|
|
13
|
+
};
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
teardown(function() {
|
|
18
|
+
myp5.remove();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
suite('p5.prototype.setHeading() RADIANS', function() {
|
|
22
|
+
setup(function() {
|
|
23
|
+
myp5.angleMode(RADIANS);
|
|
24
|
+
v = myp5.createVector(1, 1);
|
|
25
|
+
v.setHeading(1);
|
|
26
|
+
});
|
|
27
|
+
test('should have heading() value of 1 (RADIANS)', function() {
|
|
28
|
+
assert.closeTo(v.heading(), 1, 0.001);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
suite('p5.prototype.setHeading() DEGREES', function() {
|
|
33
|
+
setup(function() {
|
|
34
|
+
myp5.angleMode(DEGREES);
|
|
35
|
+
v = myp5.createVector(1, 1);
|
|
36
|
+
v.setHeading(1);
|
|
37
|
+
});
|
|
38
|
+
test('should have heading() value of 1 (DEGREES)', function() {
|
|
39
|
+
assert.closeTo(v.heading(), 1, 0.001);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
suite('p5.prototype.createVector()', function() {
|
|
44
|
+
setup(function() {
|
|
45
|
+
v = myp5.createVector();
|
|
46
|
+
});
|
|
47
|
+
test('should create instance of p5.Vector', function() {
|
|
48
|
+
assert.instanceOf(v, p5.Vector);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('should have x, y, z be initialized to 0', function() {
|
|
52
|
+
assert.equal(v.x, 0);
|
|
53
|
+
assert.equal(v.y, 0);
|
|
54
|
+
assert.equal(v.z, 0);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
suite('p5.prototype.createVector(1, 2, 3)', function() {
|
|
59
|
+
setup(function() {
|
|
60
|
+
v = myp5.createVector(1, 2, 3);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test('should have x, y, z be initialized to 1,2,3', function() {
|
|
64
|
+
assert.equal(v.x, 1);
|
|
65
|
+
assert.equal(v.y, 2);
|
|
66
|
+
assert.equal(v.z, 3);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
suite('new p5.Vector()', function() {
|
|
71
|
+
setup(function() {
|
|
72
|
+
v = new p5.Vector();
|
|
73
|
+
});
|
|
74
|
+
test('should set constant to DEGREES', function() {
|
|
75
|
+
assert.instanceOf(v, p5.Vector);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test('should have x, y, z be initialized to 0', function() {
|
|
79
|
+
assert.equal(v.x, 0);
|
|
80
|
+
assert.equal(v.y, 0);
|
|
81
|
+
assert.equal(v.z, 0);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
suite('new p5.Vector(1, 2, 3)', function() {
|
|
86
|
+
setup(function() {
|
|
87
|
+
v = new p5.Vector(1, 2, 3);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test('should have x, y, z be initialized to 1,2,3', function() {
|
|
91
|
+
assert.equal(v.x, 1);
|
|
92
|
+
assert.equal(v.y, 2);
|
|
93
|
+
assert.equal(v.z, 3);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
suite('new p5.Vector(1,2,undefined)', function() {
|
|
98
|
+
setup(function() {
|
|
99
|
+
v = new p5.Vector(1, 2, undefined);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test('should have x, y, z be initialized to 1,2,0', function() {
|
|
103
|
+
assert.equal(v.x, 1);
|
|
104
|
+
assert.equal(v.y, 2);
|
|
105
|
+
assert.equal(v.z, 0);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
suite('rotate', function() {
|
|
110
|
+
suite('p5.Vector.prototype.rotate() [INSTANCE]', function() {
|
|
111
|
+
test('should return the same object', function() {
|
|
112
|
+
v = myp5.createVector(0, 1);
|
|
113
|
+
expect(v.rotate(Math.PI)).to.eql(v);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
suite('radians', function() {
|
|
117
|
+
setup(function() {
|
|
118
|
+
myp5.angleMode(RADIANS);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test('should rotate the vector [0, 1, 0] by pi radians to [0, -1, 0]', function() {
|
|
122
|
+
v = myp5.createVector(0, 1, 0);
|
|
123
|
+
v.rotate(Math.PI);
|
|
124
|
+
expect(v.x).to.be.closeTo(0, 0.01);
|
|
125
|
+
expect(v.y).to.be.closeTo(-1, 0.01);
|
|
126
|
+
expect(v.z).to.be.closeTo(0, 0.01);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test('should rotate the vector [1, 0, 0] by -pi/2 radians to [0, -1, 0]', function() {
|
|
130
|
+
v = myp5.createVector(1, 0, 0);
|
|
131
|
+
v.rotate(-Math.PI / 2);
|
|
132
|
+
expect(v.x).to.be.closeTo(0, 0.01);
|
|
133
|
+
expect(v.y).to.be.closeTo(-1, 0.01);
|
|
134
|
+
expect(v.z).to.be.closeTo(0, 0.01);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test('should rotate the vector [1, 0, 0] by pi radians to [-1, 0, 0]', function() {
|
|
138
|
+
v = myp5.createVector(1, 0, 0);
|
|
139
|
+
v.rotate(Math.PI);
|
|
140
|
+
expect(v.x).to.be.closeTo(-1, 0.01);
|
|
141
|
+
expect(v.y).to.be.closeTo(0, 0.01);
|
|
142
|
+
expect(v.z).to.be.closeTo(0, 0.01);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
suite('degrees', function() {
|
|
147
|
+
setup(function() {
|
|
148
|
+
myp5.angleMode(DEGREES);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test('should rotate the vector [0, 1, 0] by 180 degrees to [0, -1, 0]', function() {
|
|
152
|
+
v = myp5.createVector(0, 1, 0);
|
|
153
|
+
v.rotate(180);
|
|
154
|
+
expect(v.x).to.be.closeTo(0, 0.01);
|
|
155
|
+
expect(v.y).to.be.closeTo(-1, 0.01);
|
|
156
|
+
expect(v.z).to.be.closeTo(0, 0.01);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test('should rotate the vector [1, 0, 0] by -90 degrees to [0, -1, 0]', function() {
|
|
160
|
+
v = myp5.createVector(1, 0, 0);
|
|
161
|
+
v.rotate(-90);
|
|
162
|
+
expect(v.x).to.be.closeTo(0, 0.01);
|
|
163
|
+
expect(v.y).to.be.closeTo(-1, 0.01);
|
|
164
|
+
expect(v.z).to.be.closeTo(0, 0.01);
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
suite('p5.Vector.rotate() [CLASS]', function() {
|
|
170
|
+
setup(function() {
|
|
171
|
+
myp5.angleMode(RADIANS);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test('should not change the original object', function() {
|
|
175
|
+
v = myp5.createVector(1, 0, 0);
|
|
176
|
+
p5.Vector.rotate(v, Math.PI / 2);
|
|
177
|
+
expect(v.x).to.equal(1);
|
|
178
|
+
expect(v.y).to.equal(0);
|
|
179
|
+
expect(v.z).to.equal(0);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test('should rotate the vector [0, 1, 0] by pi radians to [0, -1, 0]', function() {
|
|
183
|
+
v = myp5.createVector(0, 1, 0);
|
|
184
|
+
const v1 = p5.Vector.rotate(v, Math.PI);
|
|
185
|
+
expect(v1.x).to.be.closeTo(0, 0.01);
|
|
186
|
+
expect(v1.y).to.be.closeTo(-1, 0.01);
|
|
187
|
+
expect(v1.z).to.be.closeTo(0, 0.01);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
test('should rotate the vector [1, 0, 0] by -pi/2 radians to [0, -1, 0]', function() {
|
|
191
|
+
v = myp5.createVector(1, 0, 0);
|
|
192
|
+
const v1 = p5.Vector.rotate(v, -Math.PI / 2);
|
|
193
|
+
expect(v1.x).to.be.closeTo(0, 0.01);
|
|
194
|
+
expect(v1.y).to.be.closeTo(-1, 0.01);
|
|
195
|
+
expect(v1.z).to.be.closeTo(0, 0.01);
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
suite('angleBetween', function() {
|
|
201
|
+
let v1, v2;
|
|
202
|
+
setup(function() {
|
|
203
|
+
v1 = new p5.Vector(1, 0, 0);
|
|
204
|
+
v2 = new p5.Vector(2, 2, 0);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
suite('p5.Vector.prototype.angleBetween() [INSTANCE]', function() {
|
|
208
|
+
test('should return a Number', function() {
|
|
209
|
+
const res = v1.angleBetween(v2);
|
|
210
|
+
expect(typeof res).to.eql('number');
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
test('should not trip on rounding issues in 2D space', function() {
|
|
214
|
+
v1 = new p5.Vector(-11, -20);
|
|
215
|
+
v2 = new p5.Vector(-5.5, -10);
|
|
216
|
+
const v3 = new p5.Vector(5.5, 10);
|
|
217
|
+
|
|
218
|
+
expect(v1.angleBetween(v2)).to.be.closeTo(0, 0.00001);
|
|
219
|
+
expect(v1.angleBetween(v3)).to.be.closeTo(Math.PI, 0.00001);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
test('should not trip on rounding issues in 3D space', function() {
|
|
223
|
+
v1 = new p5.Vector(1, 1.1, 1.2);
|
|
224
|
+
v2 = new p5.Vector(2, 2.2, 2.4);
|
|
225
|
+
expect(v1.angleBetween(v2)).to.be.closeTo(0, 0.00001);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
test('should return NaN for zero vector', function() {
|
|
229
|
+
v1 = new p5.Vector(0, 0, 0);
|
|
230
|
+
v2 = new p5.Vector(2, 3, 4);
|
|
231
|
+
expect(v1.angleBetween(v2)).to.be.NaN;
|
|
232
|
+
expect(v2.angleBetween(v1)).to.be.NaN;
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
test('between [1,0,0] and [1,0,0] should be 0 degrees', function() {
|
|
236
|
+
myp5.angleMode(DEGREES);
|
|
237
|
+
v1 = myp5.createVector(1, 0, 0);
|
|
238
|
+
v2 = myp5.createVector(1, 0, 0);
|
|
239
|
+
expect(v1.angleBetween(v2)).to.equal(0);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
test('between [0,3,0] and [0,-3,0] should be 180 degrees', function() {
|
|
243
|
+
myp5.angleMode(DEGREES);
|
|
244
|
+
v1 = myp5.createVector(0, 3, 0);
|
|
245
|
+
v2 = myp5.createVector(0, -3, 0);
|
|
246
|
+
expect(v1.angleBetween(v2)).to.be.closeTo(180, 0.01);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
test('between [1,0,0] and [2,2,0] should be 1/4 PI radians', function() {
|
|
250
|
+
v1 = new p5.Vector(1, 0, 0);
|
|
251
|
+
v2 = new p5.Vector(2, 2, 0);
|
|
252
|
+
expect(v1.angleBetween(v2)).to.be.closeTo(Math.PI / 4, 0.01);
|
|
253
|
+
expect(v2.angleBetween(v1)).to.be.closeTo(-1 * Math.PI / 4, 0.01);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
test('between [2,0,0] and [-2,0,0] should be PI radians', function() {
|
|
257
|
+
v1 = new p5.Vector(2, 0, 0);
|
|
258
|
+
v2 = new p5.Vector(-2, 0, 0);
|
|
259
|
+
expect(v1.angleBetween(v2)).to.be.closeTo(Math.PI, 0.01);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
test('between [2,0,0] and [-2,-2,0] should be -3/4 PI radians ', function() {
|
|
263
|
+
v1 = new p5.Vector(2, 0, 0);
|
|
264
|
+
v2 = new p5.Vector(-2, -2, 0);
|
|
265
|
+
expect(v1.angleBetween(v2)).to.be.closeTo(
|
|
266
|
+
-1 * (Math.PI / 2 + Math.PI / 4),
|
|
267
|
+
0.01
|
|
268
|
+
);
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
test('between [-2,-2,0] and [2,0,0] should be 3/4 PI radians', function() {
|
|
272
|
+
v1 = new p5.Vector(-2, -2, 0);
|
|
273
|
+
v2 = new p5.Vector(2, 0, 0);
|
|
274
|
+
expect(v1.angleBetween(v2)).to.be.closeTo(
|
|
275
|
+
Math.PI / 2 + Math.PI / 4,
|
|
276
|
+
0.01
|
|
277
|
+
);
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
test('For the same vectors, the angle between them should always be 0.', function() {
|
|
281
|
+
v1 = myp5.createVector(288, 814);
|
|
282
|
+
v2 = myp5.createVector(288, 814);
|
|
283
|
+
expect(v1.angleBetween(v2)).to.equal(0);
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
test('The angle between vectors pointing in opposite is always PI.', function() {
|
|
287
|
+
v1 = myp5.createVector(219, 560);
|
|
288
|
+
v2 = myp5.createVector(-219, -560);
|
|
289
|
+
expect(v1.angleBetween(v2)).to.be.closeTo(Math.PI, 0.0000001);
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
suite('p5.Vector.angleBetween() [CLASS]', function() {
|
|
294
|
+
test('should return NaN for zero vector', function() {
|
|
295
|
+
v1 = new p5.Vector(0, 0, 0);
|
|
296
|
+
v2 = new p5.Vector(2, 3, 4);
|
|
297
|
+
expect(p5.Vector.angleBetween(v1, v2)).to.be.NaN;
|
|
298
|
+
expect(p5.Vector.angleBetween(v2, v1)).to.be.NaN;
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
test('between [1,0,0] and [0,-1,0] should be -90 degrees', function() {
|
|
302
|
+
myp5.angleMode(DEGREES);
|
|
303
|
+
v1 = myp5.createVector(1, 0, 0);
|
|
304
|
+
v2 = myp5.createVector(0, -1, 0);
|
|
305
|
+
expect(p5.Vector.angleBetween(v1, v2)).to.be.closeTo(-90, 0.01);
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
test('between [0,3,0] and [0,-3,0] should be PI radians', function() {
|
|
309
|
+
v1 = new p5.Vector(0, 3, 0);
|
|
310
|
+
v2 = new p5.Vector(0, -3, 0);
|
|
311
|
+
expect(p5.Vector.angleBetween(v1, v2)).to.be.closeTo(Math.PI, 0.01);
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
suite('set()', function() {
|
|
317
|
+
suite('with p5.Vector', function() {
|
|
318
|
+
test("should have x, y, z be initialized to the vector's x, y, z", function() {
|
|
319
|
+
v.set(new p5.Vector(2, 5, 6));
|
|
320
|
+
expect(v.x).to.eql(2);
|
|
321
|
+
expect(v.y).to.eql(5);
|
|
322
|
+
expect(v.z).to.eql(6);
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
suite('with Array', function() {
|
|
327
|
+
test('[2,4] should set x === 2, y === 4, z === 0', function() {
|
|
328
|
+
v.set([2, 4]);
|
|
329
|
+
expect(v.x).to.eql(2);
|
|
330
|
+
expect(v.y).to.eql(4);
|
|
331
|
+
expect(v.z).to.eql(0);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
test("should have x, y, z be initialized to the array's 0,1,2 index", function() {
|
|
335
|
+
v.set([2, 5, 6]);
|
|
336
|
+
expect(v.x).to.eql(2);
|
|
337
|
+
expect(v.y).to.eql(5);
|
|
338
|
+
expect(v.z).to.eql(6);
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
suite('set(1,2,3)', function() {
|
|
343
|
+
test('should have x, y, z be initialized to the 1, 2, 3', function() {
|
|
344
|
+
v.set(1, 2, 3);
|
|
345
|
+
expect(v.x).to.eql(1);
|
|
346
|
+
expect(v.y).to.eql(2);
|
|
347
|
+
expect(v.z).to.eql(3);
|
|
348
|
+
});
|
|
349
|
+
});
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
suite('copy', function() {
|
|
353
|
+
setup(function() {
|
|
354
|
+
v = new p5.Vector(2, 3, 4);
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
suite('p5.Vector.prototype.copy() [INSTANCE]', function() {
|
|
358
|
+
test('should not return the same instance', function() {
|
|
359
|
+
var newObject = v.copy();
|
|
360
|
+
expect(newObject).to.not.equal(v);
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
test("should return the calling object's x, y, z", function() {
|
|
364
|
+
var newObject = v.copy();
|
|
365
|
+
expect(newObject.x).to.eql(2);
|
|
366
|
+
expect(newObject.y).to.eql(3);
|
|
367
|
+
expect(newObject.z).to.eql(4);
|
|
368
|
+
});
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
suite('p5.Vector.copy() [CLASS]', function() {
|
|
372
|
+
test('should not return the same instance', function() {
|
|
373
|
+
var newObject = p5.Vector.copy(v);
|
|
374
|
+
expect(newObject).to.not.equal(v);
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
test("should return the passed object's x, y, z", function() {
|
|
378
|
+
var newObject = p5.Vector.copy(v);
|
|
379
|
+
expect(newObject.x).to.eql(2);
|
|
380
|
+
expect(newObject.y).to.eql(3);
|
|
381
|
+
expect(newObject.z).to.eql(4);
|
|
382
|
+
});
|
|
383
|
+
});
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
suite('add()', function() {
|
|
387
|
+
setup(function() {
|
|
388
|
+
v = new p5.Vector();
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
suite('with p5.Vector', function() {
|
|
392
|
+
test('should add x, y, z from the vector argument', function() {
|
|
393
|
+
v.add(new p5.Vector(1, 5, 6));
|
|
394
|
+
expect(v.x).to.eql(1);
|
|
395
|
+
expect(v.y).to.eql(5);
|
|
396
|
+
expect(v.z).to.eql(6);
|
|
397
|
+
});
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
suite('with Array', function() {
|
|
401
|
+
suite('add([2, 4])', function() {
|
|
402
|
+
test('should add the x and y components', function() {
|
|
403
|
+
v.add([2, 4]);
|
|
404
|
+
expect(v.x).to.eql(2);
|
|
405
|
+
expect(v.y).to.eql(4);
|
|
406
|
+
expect(v.z).to.eql(0);
|
|
407
|
+
});
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
test("should add the array's 0,1,2 index", function() {
|
|
411
|
+
v.add([2, 5, 6]);
|
|
412
|
+
expect(v.x).to.eql(2);
|
|
413
|
+
expect(v.y).to.eql(5);
|
|
414
|
+
expect(v.z).to.eql(6);
|
|
415
|
+
});
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
suite('add(3,5)', function() {
|
|
419
|
+
test('should add the x and y components', function() {
|
|
420
|
+
v.add(3, 5);
|
|
421
|
+
expect(v.x).to.eql(3);
|
|
422
|
+
expect(v.y).to.eql(5);
|
|
423
|
+
expect(v.z).to.eql(0);
|
|
424
|
+
});
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
suite('add(2,3,4)', function() {
|
|
428
|
+
test('should add the x, y, z components', function() {
|
|
429
|
+
v.add(5, 5, 5);
|
|
430
|
+
expect(v.x).to.eql(5);
|
|
431
|
+
expect(v.y).to.eql(5);
|
|
432
|
+
expect(v.z).to.eql(5);
|
|
433
|
+
});
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
suite('p5.Vector.add(v1, v2)', function() {
|
|
437
|
+
var v1, v2, res;
|
|
438
|
+
setup(function() {
|
|
439
|
+
v1 = new p5.Vector(2, 0, 3);
|
|
440
|
+
v2 = new p5.Vector(0, 1, 3);
|
|
441
|
+
res = p5.Vector.add(v1, v2);
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
test('should return neither v1 nor v2', function() {
|
|
445
|
+
expect(res).to.not.eql(v1);
|
|
446
|
+
expect(res).to.not.eql(v2);
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
test('should be sum of the two p5.Vectors', function() {
|
|
450
|
+
expect(res.x).to.eql(v1.x + v2.x);
|
|
451
|
+
expect(res.y).to.eql(v1.y + v2.y);
|
|
452
|
+
expect(res.z).to.eql(v1.z + v2.z);
|
|
453
|
+
});
|
|
454
|
+
});
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
suite('rem()', function() {
|
|
458
|
+
setup(function() {
|
|
459
|
+
v = myp5.createVector(3, 4, 5);
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
test('should give same vector if nothing passed as parameter', function() {
|
|
463
|
+
v.rem();
|
|
464
|
+
expect(v.x).to.eql(3);
|
|
465
|
+
expect(v.y).to.eql(4);
|
|
466
|
+
expect(v.z).to.eql(5);
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
test('should give correct output if passed only one numeric value', function() {
|
|
470
|
+
v.rem(2);
|
|
471
|
+
expect(v.x).to.eql(1);
|
|
472
|
+
expect(v.y).to.eql(0);
|
|
473
|
+
expect(v.z).to.eql(1);
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
test('should give correct output if passed two numeric value', function() {
|
|
477
|
+
v.rem(2, 3);
|
|
478
|
+
expect(v.x).to.eql(1);
|
|
479
|
+
expect(v.y).to.eql(1);
|
|
480
|
+
expect(v.z).to.eql(5);
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
test('should give correct output if passed three numeric value', function() {
|
|
484
|
+
v.rem(2, 3, 4);
|
|
485
|
+
expect(v.x).to.eql(1);
|
|
486
|
+
expect(v.y).to.eql(1);
|
|
487
|
+
expect(v.z).to.eql(1);
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
suite('with p5.Vector', function() {
|
|
491
|
+
test('should return correct output if only one component is non-zero', function() {
|
|
492
|
+
v.rem(new p5.Vector(0, 0, 4));
|
|
493
|
+
expect(v.x).to.eql(3);
|
|
494
|
+
expect(v.y).to.eql(4);
|
|
495
|
+
expect(v.z).to.eql(1);
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
test('should return correct output if x component is zero', () => {
|
|
499
|
+
v.rem(new p5.Vector(0, 3, 4));
|
|
500
|
+
expect(v.x).to.eql(3);
|
|
501
|
+
expect(v.y).to.eql(1);
|
|
502
|
+
expect(v.z).to.eql(1);
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
test('should return correct output if all components are non-zero', () => {
|
|
506
|
+
v.rem(new p5.Vector(2, 3, 4));
|
|
507
|
+
expect(v.x).to.eql(1);
|
|
508
|
+
expect(v.y).to.eql(1);
|
|
509
|
+
expect(v.z).to.eql(1);
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
test('should return same vector if all components are zero', () => {
|
|
513
|
+
v.rem(new p5.Vector(0, 0, 0));
|
|
514
|
+
expect(v.x).to.eql(3);
|
|
515
|
+
expect(v.y).to.eql(4);
|
|
516
|
+
expect(v.z).to.eql(5);
|
|
517
|
+
});
|
|
518
|
+
});
|
|
519
|
+
|
|
520
|
+
suite('with negative vectors', function() {
|
|
521
|
+
let v;
|
|
522
|
+
setup(function() {
|
|
523
|
+
v = new p5.Vector(-15, -5, -2);
|
|
524
|
+
});
|
|
525
|
+
test('should return correct output', () => {
|
|
526
|
+
v.rem(new p5.Vector(2, 3, 3));
|
|
527
|
+
expect(v.x).to.eql(-1);
|
|
528
|
+
expect(v.y).to.eql(-2);
|
|
529
|
+
expect(v.z).to.eql(-2);
|
|
530
|
+
});
|
|
531
|
+
});
|
|
532
|
+
|
|
533
|
+
suite('with Arrays', function() {
|
|
534
|
+
test('should return remainder of vector components for 3D vector', function() {
|
|
535
|
+
v.rem([2, 3, 0]);
|
|
536
|
+
expect(v.x).to.eql(1);
|
|
537
|
+
expect(v.y).to.eql(1);
|
|
538
|
+
expect(v.z).to.eql(5);
|
|
539
|
+
});
|
|
540
|
+
test('should return remainder of vector components for 2D vector', function() {
|
|
541
|
+
v.rem([2, 3]);
|
|
542
|
+
expect(v.x).to.eql(1);
|
|
543
|
+
expect(v.y).to.eql(1);
|
|
544
|
+
expect(v.z).to.eql(5);
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
test('should return correct output if x,y components are zero for 2D vector', () => {
|
|
548
|
+
v.rem([0, 0]);
|
|
549
|
+
expect(v.x).to.eql(3);
|
|
550
|
+
expect(v.y).to.eql(4);
|
|
551
|
+
expect(v.z).to.eql(5);
|
|
552
|
+
});
|
|
553
|
+
|
|
554
|
+
test('should return same vector if any vector component is non-finite number', () => {
|
|
555
|
+
v.rem([2, 3, Infinity]);
|
|
556
|
+
expect(v.x).to.eql(3);
|
|
557
|
+
expect(v.y).to.eql(4);
|
|
558
|
+
expect(v.z).to.eql(5);
|
|
559
|
+
});
|
|
560
|
+
});
|
|
561
|
+
|
|
562
|
+
suite('p5.Vector.rem(v1,v2)', function() {
|
|
563
|
+
let v1, v2, res;
|
|
564
|
+
setup(function() {
|
|
565
|
+
v1 = new p5.Vector(2, 3, 4);
|
|
566
|
+
v2 = new p5.Vector(1, 2, 3);
|
|
567
|
+
res = p5.Vector.rem(v1, v2);
|
|
568
|
+
});
|
|
569
|
+
|
|
570
|
+
test('should return neither v1 nor v2', function() {
|
|
571
|
+
expect(res).to.not.eql(v1);
|
|
572
|
+
expect(res).to.not.eql(v2);
|
|
573
|
+
});
|
|
574
|
+
|
|
575
|
+
test('should be v1 % v2', function() {
|
|
576
|
+
expect(res.x).to.eql(v1.x % v2.x);
|
|
577
|
+
expect(res.y).to.eql(v1.y % v2.y);
|
|
578
|
+
expect(res.z).to.eql(v1.z % v2.z);
|
|
579
|
+
});
|
|
580
|
+
});
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
suite('sub()', function() {
|
|
584
|
+
setup(function() {
|
|
585
|
+
v.x = 0;
|
|
586
|
+
v.y = 0;
|
|
587
|
+
v.z = 0;
|
|
588
|
+
});
|
|
589
|
+
suite('with p5.Vector', function() {
|
|
590
|
+
test('should sub x, y, z from the vector argument', function() {
|
|
591
|
+
v.sub(new p5.Vector(2, 5, 6));
|
|
592
|
+
expect(v.x).to.eql(-2);
|
|
593
|
+
expect(v.y).to.eql(-5);
|
|
594
|
+
expect(v.z).to.eql(-6);
|
|
595
|
+
});
|
|
596
|
+
});
|
|
597
|
+
|
|
598
|
+
suite('with Array', function() {
|
|
599
|
+
suite('sub([2, 4])', function() {
|
|
600
|
+
test('should sub the x and y components', function() {
|
|
601
|
+
v.sub([2, 4]);
|
|
602
|
+
expect(v.x).to.eql(-2);
|
|
603
|
+
expect(v.y).to.eql(-4);
|
|
604
|
+
expect(v.z).to.eql(0);
|
|
605
|
+
});
|
|
606
|
+
});
|
|
607
|
+
|
|
608
|
+
test("should subtract from the array's 0,1,2 index", function() {
|
|
609
|
+
v.sub([2, 5, 6]);
|
|
610
|
+
expect(v.x).to.eql(-2);
|
|
611
|
+
expect(v.y).to.eql(-5);
|
|
612
|
+
expect(v.z).to.eql(-6);
|
|
613
|
+
});
|
|
614
|
+
});
|
|
615
|
+
|
|
616
|
+
suite('sub(3,5)', function() {
|
|
617
|
+
test('should subtract the x and y components', function() {
|
|
618
|
+
v.sub(3, 5);
|
|
619
|
+
expect(v.x).to.eql(-3);
|
|
620
|
+
expect(v.y).to.eql(-5);
|
|
621
|
+
expect(v.z).to.eql(0);
|
|
622
|
+
});
|
|
623
|
+
});
|
|
624
|
+
|
|
625
|
+
suite('sub(2,3,4)', function() {
|
|
626
|
+
test('should subtract the x, y, z components', function() {
|
|
627
|
+
v.sub(5, 5, 5);
|
|
628
|
+
expect(v.x).to.eql(-5);
|
|
629
|
+
expect(v.y).to.eql(-5);
|
|
630
|
+
expect(v.z).to.eql(-5);
|
|
631
|
+
});
|
|
632
|
+
});
|
|
633
|
+
|
|
634
|
+
suite('p5.Vector.sub(v1, v2)', function() {
|
|
635
|
+
var v1, v2, res;
|
|
636
|
+
setup(function() {
|
|
637
|
+
v1 = new p5.Vector(2, 0, 3);
|
|
638
|
+
v2 = new p5.Vector(0, 1, 3);
|
|
639
|
+
res = p5.Vector.sub(v1, v2);
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
test('should return neither v1 nor v2', function() {
|
|
643
|
+
expect(res).to.not.eql(v1);
|
|
644
|
+
expect(res).to.not.eql(v2);
|
|
645
|
+
});
|
|
646
|
+
|
|
647
|
+
test('should be v1 - v2', function() {
|
|
648
|
+
expect(res.x).to.eql(v1.x - v2.x);
|
|
649
|
+
expect(res.y).to.eql(v1.y - v2.y);
|
|
650
|
+
expect(res.z).to.eql(v1.z - v2.z);
|
|
651
|
+
});
|
|
652
|
+
});
|
|
653
|
+
});
|
|
654
|
+
|
|
655
|
+
suite('mult()', function() {
|
|
656
|
+
setup(function() {
|
|
657
|
+
v = new p5.Vector(1, 1, 1);
|
|
658
|
+
});
|
|
659
|
+
|
|
660
|
+
test('should return the same object', function() {
|
|
661
|
+
expect(v.mult(1)).to.eql(v);
|
|
662
|
+
});
|
|
663
|
+
|
|
664
|
+
test('should not change x, y, z if no argument is given', function() {
|
|
665
|
+
v.mult();
|
|
666
|
+
expect(v.x).to.eql(1);
|
|
667
|
+
expect(v.y).to.eql(1);
|
|
668
|
+
expect(v.z).to.eql(1);
|
|
669
|
+
});
|
|
670
|
+
|
|
671
|
+
test('should not change x, y, z if n is not a finite number', function() {
|
|
672
|
+
v.mult(NaN);
|
|
673
|
+
expect(v.x).to.eql(1);
|
|
674
|
+
expect(v.y).to.eql(1);
|
|
675
|
+
expect(v.z).to.eql(1);
|
|
676
|
+
});
|
|
677
|
+
|
|
678
|
+
suite('with scalar', function() {
|
|
679
|
+
test('multiply the x, y, z with the scalar', function() {
|
|
680
|
+
v.mult(2);
|
|
681
|
+
expect(v.x).to.eql(2);
|
|
682
|
+
expect(v.y).to.eql(2);
|
|
683
|
+
expect(v.z).to.eql(2);
|
|
684
|
+
});
|
|
685
|
+
});
|
|
686
|
+
|
|
687
|
+
suite('v0.mult(v1)', function() {
|
|
688
|
+
var v0, v1;
|
|
689
|
+
setup(function() {
|
|
690
|
+
v0 = new p5.Vector(1, 2, 3);
|
|
691
|
+
v1 = new p5.Vector(2, 3, 4);
|
|
692
|
+
v0.mult(v1);
|
|
693
|
+
});
|
|
694
|
+
|
|
695
|
+
test('should do component wise multiplication', function() {
|
|
696
|
+
expect(v0.x).to.eql(2);
|
|
697
|
+
expect(v0.y).to.eql(6);
|
|
698
|
+
expect(v0.z).to.eql(12);
|
|
699
|
+
});
|
|
700
|
+
});
|
|
701
|
+
|
|
702
|
+
suite('v0.mult(arr)', function() {
|
|
703
|
+
var v0, arr;
|
|
704
|
+
setup(function() {
|
|
705
|
+
v0 = new p5.Vector(1, 2, 3);
|
|
706
|
+
arr = [2, 3, 4];
|
|
707
|
+
v0.mult(arr);
|
|
708
|
+
});
|
|
709
|
+
|
|
710
|
+
test('should do component wise multiplication from an array', function() {
|
|
711
|
+
expect(v0.x).to.eql(2);
|
|
712
|
+
expect(v0.y).to.eql(6);
|
|
713
|
+
expect(v0.z).to.eql(12);
|
|
714
|
+
});
|
|
715
|
+
});
|
|
716
|
+
|
|
717
|
+
suite('p5.Vector.mult(v, n)', function() {
|
|
718
|
+
var v, res;
|
|
719
|
+
setup(function() {
|
|
720
|
+
v = new p5.Vector(1, 2, 3);
|
|
721
|
+
res = p5.Vector.mult(v, 4);
|
|
722
|
+
});
|
|
723
|
+
|
|
724
|
+
test('should return a new p5.Vector', function() {
|
|
725
|
+
expect(res).to.not.eql(v);
|
|
726
|
+
});
|
|
727
|
+
|
|
728
|
+
test('should multiply the scalar', function() {
|
|
729
|
+
expect(res.x).to.eql(4);
|
|
730
|
+
expect(res.y).to.eql(8);
|
|
731
|
+
expect(res.z).to.eql(12);
|
|
732
|
+
});
|
|
733
|
+
});
|
|
734
|
+
|
|
735
|
+
suite('p5.Vector.mult(v, v', function() {
|
|
736
|
+
var v0, v1, res;
|
|
737
|
+
setup(function() {
|
|
738
|
+
v0 = new p5.Vector(1, 2, 3);
|
|
739
|
+
v1 = new p5.Vector(2, 3, 4);
|
|
740
|
+
res = p5.Vector.mult(v0, v1);
|
|
741
|
+
});
|
|
742
|
+
|
|
743
|
+
test('should return new vector from component wise multiplication', function() {
|
|
744
|
+
expect(res.x).to.eql(2);
|
|
745
|
+
expect(res.y).to.eql(6);
|
|
746
|
+
expect(res.z).to.eql(12);
|
|
747
|
+
});
|
|
748
|
+
});
|
|
749
|
+
|
|
750
|
+
suite('p5.Vector.mult(v, arr', function() {
|
|
751
|
+
var v0, arr, res;
|
|
752
|
+
setup(function() {
|
|
753
|
+
v0 = new p5.Vector(1, 2, 3);
|
|
754
|
+
arr = [2, 3, 4];
|
|
755
|
+
res = p5.Vector.mult(v0, arr);
|
|
756
|
+
});
|
|
757
|
+
|
|
758
|
+
test('should return new vector from component wise multiplication with an array', function() {
|
|
759
|
+
expect(res.x).to.eql(2);
|
|
760
|
+
expect(res.y).to.eql(6);
|
|
761
|
+
expect(res.z).to.eql(12);
|
|
762
|
+
});
|
|
763
|
+
});
|
|
764
|
+
});
|
|
765
|
+
|
|
766
|
+
suite('div()', function() {
|
|
767
|
+
setup(function() {
|
|
768
|
+
v = new p5.Vector(1, 1, 1);
|
|
769
|
+
});
|
|
770
|
+
|
|
771
|
+
test('should return the same object', function() {
|
|
772
|
+
expect(v.div(1)).to.eql(v);
|
|
773
|
+
});
|
|
774
|
+
|
|
775
|
+
test('should not change x, y, z if no argument is given', function() {
|
|
776
|
+
v.div();
|
|
777
|
+
expect(v.x).to.eql(1);
|
|
778
|
+
expect(v.y).to.eql(1);
|
|
779
|
+
expect(v.z).to.eql(1);
|
|
780
|
+
});
|
|
781
|
+
|
|
782
|
+
test('should not change x, y, z if n is not a finite number', function() {
|
|
783
|
+
v.div(NaN);
|
|
784
|
+
expect(v.x).to.eql(1);
|
|
785
|
+
expect(v.y).to.eql(1);
|
|
786
|
+
expect(v.z).to.eql(1);
|
|
787
|
+
});
|
|
788
|
+
|
|
789
|
+
suite('with scalar', function() {
|
|
790
|
+
test('divide the x, y, z with the scalar', function() {
|
|
791
|
+
v.div(2);
|
|
792
|
+
expect(v.x).to.be.closeTo(0.5, 0.01);
|
|
793
|
+
expect(v.y).to.be.closeTo(0.5, 0.01);
|
|
794
|
+
expect(v.z).to.be.closeTo(0.5, 0.01);
|
|
795
|
+
});
|
|
796
|
+
|
|
797
|
+
test('should not change x, y, z if n is 0', function() {
|
|
798
|
+
v.div(0);
|
|
799
|
+
expect(v.x).to.eql(1);
|
|
800
|
+
expect(v.y).to.eql(1);
|
|
801
|
+
expect(v.z).to.eql(1);
|
|
802
|
+
});
|
|
803
|
+
});
|
|
804
|
+
|
|
805
|
+
suite('p5.Vector.div(v, n)', function() {
|
|
806
|
+
var v, res;
|
|
807
|
+
setup(function() {
|
|
808
|
+
v = new p5.Vector(1, 1, 1);
|
|
809
|
+
res = p5.Vector.div(v, 4);
|
|
810
|
+
});
|
|
811
|
+
|
|
812
|
+
test('should not be undefined', function() {
|
|
813
|
+
expect(res).to.not.eql(undefined);
|
|
814
|
+
});
|
|
815
|
+
|
|
816
|
+
test('should return a new p5.Vector', function() {
|
|
817
|
+
expect(res).to.not.eql(v);
|
|
818
|
+
});
|
|
819
|
+
|
|
820
|
+
test('should divide the scalar', function() {
|
|
821
|
+
expect(res.x).to.eql(0.25);
|
|
822
|
+
expect(res.y).to.eql(0.25);
|
|
823
|
+
expect(res.z).to.eql(0.25);
|
|
824
|
+
});
|
|
825
|
+
});
|
|
826
|
+
|
|
827
|
+
suite('v0.div(v1)', function() {
|
|
828
|
+
var v0, v1, v2, v3;
|
|
829
|
+
setup(function() {
|
|
830
|
+
v0 = new p5.Vector(2, 6, 9);
|
|
831
|
+
v1 = new p5.Vector(2, 2, 3);
|
|
832
|
+
v2 = new p5.Vector(1, 1, 1);
|
|
833
|
+
v3 = new p5.Vector(0, 0, 0);
|
|
834
|
+
|
|
835
|
+
v0.div(v1);
|
|
836
|
+
});
|
|
837
|
+
|
|
838
|
+
test('should do component wise division', function() {
|
|
839
|
+
expect(v0.x).to.eql(1);
|
|
840
|
+
expect(v0.y).to.eql(3);
|
|
841
|
+
expect(v0.z).to.eql(3);
|
|
842
|
+
});
|
|
843
|
+
|
|
844
|
+
test('should not change x, y, z if v3 is all 0', function() {
|
|
845
|
+
v2.div(v3);
|
|
846
|
+
expect(v2.x).to.eql(1);
|
|
847
|
+
expect(v2.y).to.eql(1);
|
|
848
|
+
expect(v2.z).to.eql(1);
|
|
849
|
+
});
|
|
850
|
+
|
|
851
|
+
test('should work on 2D vectors', function() {
|
|
852
|
+
const v = new p5.Vector(1, 1);
|
|
853
|
+
const divisor = new p5.Vector(2, 2);
|
|
854
|
+
v.div(divisor);
|
|
855
|
+
expect(v.x).to.eql(0.5);
|
|
856
|
+
expect(v.y).to.eql(0.5);
|
|
857
|
+
expect(v.z).to.eql(0);
|
|
858
|
+
});
|
|
859
|
+
|
|
860
|
+
test('should work when the dividend has 0', function() {
|
|
861
|
+
const v = new p5.Vector(1, 0);
|
|
862
|
+
const divisor = new p5.Vector(2, 2);
|
|
863
|
+
v.div(divisor);
|
|
864
|
+
expect(v.x).to.eql(0.5);
|
|
865
|
+
expect(v.y).to.eql(0);
|
|
866
|
+
expect(v.z).to.eql(0);
|
|
867
|
+
});
|
|
868
|
+
|
|
869
|
+
test('should do nothing when the divisor has 0', function() {
|
|
870
|
+
const v = new p5.Vector(1, 1);
|
|
871
|
+
const divisor = new p5.Vector(0, 2);
|
|
872
|
+
v.div(divisor);
|
|
873
|
+
expect(v.x).to.eql(1);
|
|
874
|
+
expect(v.y).to.eql(1);
|
|
875
|
+
expect(v.z).to.eql(0);
|
|
876
|
+
});
|
|
877
|
+
});
|
|
878
|
+
|
|
879
|
+
suite('v0.div(arr)', function() {
|
|
880
|
+
var v0, v1, arr;
|
|
881
|
+
setup(function() {
|
|
882
|
+
v0 = new p5.Vector(2, 6, 9);
|
|
883
|
+
v1 = new p5.Vector(1, 1, 1);
|
|
884
|
+
arr = [2, 2, 3];
|
|
885
|
+
v0.div(arr);
|
|
886
|
+
});
|
|
887
|
+
|
|
888
|
+
test('should do component wise division with an array', function() {
|
|
889
|
+
expect(v0.x).to.eql(1);
|
|
890
|
+
expect(v0.y).to.eql(3);
|
|
891
|
+
expect(v0.z).to.eql(3);
|
|
892
|
+
});
|
|
893
|
+
|
|
894
|
+
test('should not change x, y, z if array contains 0', function() {
|
|
895
|
+
v1.div([0, 0, 0]);
|
|
896
|
+
expect(v1.x).to.eql(1);
|
|
897
|
+
expect(v1.y).to.eql(1);
|
|
898
|
+
expect(v1.z).to.eql(1);
|
|
899
|
+
});
|
|
900
|
+
});
|
|
901
|
+
|
|
902
|
+
suite('p5.Vector.div(v, v', function() {
|
|
903
|
+
var v0, v1, res;
|
|
904
|
+
setup(function() {
|
|
905
|
+
v0 = new p5.Vector(2, 6, 9);
|
|
906
|
+
v1 = new p5.Vector(2, 2, 3);
|
|
907
|
+
res = p5.Vector.div(v0, v1);
|
|
908
|
+
});
|
|
909
|
+
|
|
910
|
+
test('should return new vector from component wise division', function() {
|
|
911
|
+
expect(res.x).to.eql(1);
|
|
912
|
+
expect(res.y).to.eql(3);
|
|
913
|
+
expect(res.z).to.eql(3);
|
|
914
|
+
});
|
|
915
|
+
});
|
|
916
|
+
|
|
917
|
+
suite('p5.Vector.div(v, arr', function() {
|
|
918
|
+
var v0, arr, res;
|
|
919
|
+
setup(function() {
|
|
920
|
+
v0 = new p5.Vector(2, 6, 9);
|
|
921
|
+
arr = [2, 2, 3];
|
|
922
|
+
res = p5.Vector.div(v0, arr);
|
|
923
|
+
});
|
|
924
|
+
|
|
925
|
+
test('should return new vector from component wise division with an array', function() {
|
|
926
|
+
expect(res.x).to.eql(1);
|
|
927
|
+
expect(res.y).to.eql(3);
|
|
928
|
+
expect(res.z).to.eql(3);
|
|
929
|
+
});
|
|
930
|
+
});
|
|
931
|
+
});
|
|
932
|
+
|
|
933
|
+
suite('dot', function() {
|
|
934
|
+
setup(function() {
|
|
935
|
+
v.x = 1;
|
|
936
|
+
v.y = 1;
|
|
937
|
+
v.z = 1;
|
|
938
|
+
});
|
|
939
|
+
|
|
940
|
+
test('should return a number', function() {
|
|
941
|
+
expect(typeof v.dot(new p5.Vector()) === 'number').to.eql(true);
|
|
942
|
+
});
|
|
943
|
+
|
|
944
|
+
suite('with p5.Vector', function() {
|
|
945
|
+
test('should be the dot product of the vector', function() {
|
|
946
|
+
expect(v.dot(new p5.Vector(2, 2))).to.eql(4);
|
|
947
|
+
});
|
|
948
|
+
});
|
|
949
|
+
|
|
950
|
+
suite('with x, y, z', function() {
|
|
951
|
+
test('should be the dot product with x, y', function() {
|
|
952
|
+
expect(v.dot(2, 2)).to.eql(4);
|
|
953
|
+
});
|
|
954
|
+
|
|
955
|
+
test('should be the dot product with x, y, z', function() {
|
|
956
|
+
expect(v.dot(2, 2, 2)).to.eql(6);
|
|
957
|
+
});
|
|
958
|
+
});
|
|
959
|
+
|
|
960
|
+
suite('p5.Vector.dot(v, n)', function() {
|
|
961
|
+
var v1, v2, res;
|
|
962
|
+
setup(function() {
|
|
963
|
+
v1 = new p5.Vector(1, 1, 1);
|
|
964
|
+
v2 = new p5.Vector(2, 3, 4);
|
|
965
|
+
res = p5.Vector.dot(v1, v2);
|
|
966
|
+
});
|
|
967
|
+
|
|
968
|
+
test('should return a number', function() {
|
|
969
|
+
expect(typeof res === 'number').to.eql(true);
|
|
970
|
+
});
|
|
971
|
+
|
|
972
|
+
test('should be the dot product of the two vectors', function() {
|
|
973
|
+
expect(res).to.eql(9);
|
|
974
|
+
});
|
|
975
|
+
});
|
|
976
|
+
});
|
|
977
|
+
|
|
978
|
+
suite('cross', function() {
|
|
979
|
+
var res;
|
|
980
|
+
setup(function() {
|
|
981
|
+
v.x = 1;
|
|
982
|
+
v.y = 1;
|
|
983
|
+
v.z = 1;
|
|
984
|
+
});
|
|
985
|
+
|
|
986
|
+
test('should return a new product', function() {
|
|
987
|
+
expect(v.cross(new p5.Vector())).to.not.eql(v);
|
|
988
|
+
});
|
|
989
|
+
|
|
990
|
+
suite('with p5.Vector', function() {
|
|
991
|
+
test('should cross x, y, z from the vector argument', function() {
|
|
992
|
+
res = v.cross(new p5.Vector(2, 5, 6));
|
|
993
|
+
expect(res.x).to.eql(1); //this.y * v.z - this.z * v.y
|
|
994
|
+
expect(res.y).to.eql(-4); //this.z * v.x - this.x * v.z
|
|
995
|
+
expect(res.z).to.eql(3); //this.x * v.y - this.y * v.x
|
|
996
|
+
});
|
|
997
|
+
});
|
|
998
|
+
|
|
999
|
+
suite('p5.Vector.cross(v1, v2)', function() {
|
|
1000
|
+
var v1, v2, res;
|
|
1001
|
+
setup(function() {
|
|
1002
|
+
v1 = new p5.Vector(3, 6, 9);
|
|
1003
|
+
v2 = new p5.Vector(1, 1, 1);
|
|
1004
|
+
res = p5.Vector.cross(v1, v2);
|
|
1005
|
+
});
|
|
1006
|
+
|
|
1007
|
+
test('should not be undefined', function() {
|
|
1008
|
+
expect(res).to.not.eql(undefined);
|
|
1009
|
+
});
|
|
1010
|
+
|
|
1011
|
+
test('should return neither v1 nor v2', function() {
|
|
1012
|
+
expect(res).to.not.eql(v1);
|
|
1013
|
+
expect(res).to.not.eql(v2);
|
|
1014
|
+
});
|
|
1015
|
+
|
|
1016
|
+
test('should the cross product of v1 and v2', function() {
|
|
1017
|
+
expect(res.x).to.eql(-3);
|
|
1018
|
+
expect(res.y).to.eql(6);
|
|
1019
|
+
expect(res.z).to.eql(-3);
|
|
1020
|
+
});
|
|
1021
|
+
});
|
|
1022
|
+
});
|
|
1023
|
+
|
|
1024
|
+
suite('dist', function() {
|
|
1025
|
+
var b, c;
|
|
1026
|
+
setup(function() {
|
|
1027
|
+
v.x = 0;
|
|
1028
|
+
v.y = 0;
|
|
1029
|
+
v.z = 1;
|
|
1030
|
+
b = new p5.Vector(0, 0, 5);
|
|
1031
|
+
c = new p5.Vector(3, 4, 1);
|
|
1032
|
+
});
|
|
1033
|
+
|
|
1034
|
+
test('should return a number', function() {
|
|
1035
|
+
expect(typeof v.dist(b) === 'number').to.eql(true);
|
|
1036
|
+
});
|
|
1037
|
+
|
|
1038
|
+
test('should return distance between two vectors', function() {
|
|
1039
|
+
expect(v.dist(b)).to.eql(4);
|
|
1040
|
+
});
|
|
1041
|
+
|
|
1042
|
+
test('should return distance between two vectors', function() {
|
|
1043
|
+
expect(v.dist(c)).to.eql(5);
|
|
1044
|
+
});
|
|
1045
|
+
|
|
1046
|
+
test('should be commutative', function() {
|
|
1047
|
+
expect(b.dist(c)).to.eql(c.dist(b));
|
|
1048
|
+
});
|
|
1049
|
+
});
|
|
1050
|
+
|
|
1051
|
+
suite('p5.Vector.dist(v1, v2)', function() {
|
|
1052
|
+
var v1, v2;
|
|
1053
|
+
setup(function() {
|
|
1054
|
+
v1 = new p5.Vector(0, 0, 0);
|
|
1055
|
+
v2 = new p5.Vector(0, 3, 4);
|
|
1056
|
+
});
|
|
1057
|
+
|
|
1058
|
+
test('should return a number', function() {
|
|
1059
|
+
expect(typeof p5.Vector.dist(v1, v2) === 'number').to.eql(true);
|
|
1060
|
+
});
|
|
1061
|
+
|
|
1062
|
+
test('should be commutative', function() {
|
|
1063
|
+
expect(p5.Vector.dist(v1, v2)).to.eql(p5.Vector.dist(v2, v1));
|
|
1064
|
+
});
|
|
1065
|
+
});
|
|
1066
|
+
|
|
1067
|
+
suite('normalize', function() {
|
|
1068
|
+
suite('p5.Vector.prototype.normalize() [INSTANCE]', function() {
|
|
1069
|
+
setup(function() {
|
|
1070
|
+
v = myp5.createVector(1, 1, 1);
|
|
1071
|
+
});
|
|
1072
|
+
|
|
1073
|
+
test('should return the same object', function() {
|
|
1074
|
+
expect(v.normalize()).to.eql(v);
|
|
1075
|
+
});
|
|
1076
|
+
|
|
1077
|
+
test('unit vector should not change values', function() {
|
|
1078
|
+
v.x = 1;
|
|
1079
|
+
v.y = 0;
|
|
1080
|
+
v.z = 0;
|
|
1081
|
+
v.normalize();
|
|
1082
|
+
expect(v.x).to.eql(1);
|
|
1083
|
+
expect(v.y).to.eql(0);
|
|
1084
|
+
expect(v.z).to.eql(0);
|
|
1085
|
+
});
|
|
1086
|
+
|
|
1087
|
+
test('2,2,1 should normalize to ~0.66,0.66,0.33', function() {
|
|
1088
|
+
v.x = 2;
|
|
1089
|
+
v.y = 2;
|
|
1090
|
+
v.z = 1;
|
|
1091
|
+
v.normalize();
|
|
1092
|
+
expect(v.x).to.be.closeTo(0.6666, 0.01);
|
|
1093
|
+
expect(v.y).to.be.closeTo(0.6666, 0.01);
|
|
1094
|
+
expect(v.z).to.be.closeTo(0.3333, 0.01);
|
|
1095
|
+
});
|
|
1096
|
+
});
|
|
1097
|
+
|
|
1098
|
+
suite('p5.Vector.normalize(v) [CLASS]', function() {
|
|
1099
|
+
var res;
|
|
1100
|
+
setup(function() {
|
|
1101
|
+
v = myp5.createVector(1, 0, 0);
|
|
1102
|
+
res = p5.Vector.normalize(v);
|
|
1103
|
+
});
|
|
1104
|
+
|
|
1105
|
+
test('should not be undefined', function() {
|
|
1106
|
+
expect(res).to.not.eql(undefined);
|
|
1107
|
+
});
|
|
1108
|
+
|
|
1109
|
+
test('should not return same object v', function() {
|
|
1110
|
+
expect(res).to.not.equal(v);
|
|
1111
|
+
});
|
|
1112
|
+
|
|
1113
|
+
test('unit vector 1,0,0 should normalize to 1,0,0', function() {
|
|
1114
|
+
expect(res.x).to.eql(1);
|
|
1115
|
+
expect(res.y).to.eql(0);
|
|
1116
|
+
expect(res.z).to.eql(0);
|
|
1117
|
+
});
|
|
1118
|
+
|
|
1119
|
+
test('2,2,1 should normalize to ~0.66,0.66,0.33', function() {
|
|
1120
|
+
v.x = 2;
|
|
1121
|
+
v.y = 2;
|
|
1122
|
+
v.z = 1;
|
|
1123
|
+
res = p5.Vector.normalize(v);
|
|
1124
|
+
expect(res.x).to.be.closeTo(0.6666, 0.01);
|
|
1125
|
+
expect(res.y).to.be.closeTo(0.6666, 0.01);
|
|
1126
|
+
expect(res.z).to.be.closeTo(0.3333, 0.01);
|
|
1127
|
+
});
|
|
1128
|
+
});
|
|
1129
|
+
});
|
|
1130
|
+
|
|
1131
|
+
suite('limit', function() {
|
|
1132
|
+
let v;
|
|
1133
|
+
|
|
1134
|
+
setup(function() {
|
|
1135
|
+
v = new p5.Vector(5, 5, 5);
|
|
1136
|
+
});
|
|
1137
|
+
|
|
1138
|
+
suite('p5.Vector.prototype.limit() [INSTANCE]', function() {
|
|
1139
|
+
test('should return the same object', function() {
|
|
1140
|
+
expect(v.limit()).to.equal(v);
|
|
1141
|
+
});
|
|
1142
|
+
|
|
1143
|
+
suite('with a vector larger than the limit', function() {
|
|
1144
|
+
test('should limit the vector', function() {
|
|
1145
|
+
v.limit(1);
|
|
1146
|
+
expect(v.x).to.be.closeTo(0.5773, 0.01);
|
|
1147
|
+
expect(v.y).to.be.closeTo(0.5773, 0.01);
|
|
1148
|
+
expect(v.z).to.be.closeTo(0.5773, 0.01);
|
|
1149
|
+
});
|
|
1150
|
+
});
|
|
1151
|
+
|
|
1152
|
+
suite('with a vector smaller than the limit', function() {
|
|
1153
|
+
test('should not limit the vector', function() {
|
|
1154
|
+
v.limit(8.67);
|
|
1155
|
+
expect(v.x).to.eql(5);
|
|
1156
|
+
expect(v.y).to.eql(5);
|
|
1157
|
+
expect(v.z).to.eql(5);
|
|
1158
|
+
});
|
|
1159
|
+
});
|
|
1160
|
+
});
|
|
1161
|
+
|
|
1162
|
+
suite('p5.Vector.limit() [CLASS]', function() {
|
|
1163
|
+
test('should not return the same object', function() {
|
|
1164
|
+
expect(p5.Vector.limit(v)).to.not.equal(v);
|
|
1165
|
+
});
|
|
1166
|
+
|
|
1167
|
+
suite('with a vector larger than the limit', function() {
|
|
1168
|
+
test('should limit the vector', function() {
|
|
1169
|
+
const res = p5.Vector.limit(v, 1);
|
|
1170
|
+
expect(res.x).to.be.closeTo(0.5773, 0.01);
|
|
1171
|
+
expect(res.y).to.be.closeTo(0.5773, 0.01);
|
|
1172
|
+
expect(res.z).to.be.closeTo(0.5773, 0.01);
|
|
1173
|
+
});
|
|
1174
|
+
});
|
|
1175
|
+
|
|
1176
|
+
suite('with a vector smaller than the limit', function() {
|
|
1177
|
+
test('should not limit the vector', function() {
|
|
1178
|
+
const res = p5.Vector.limit(v, 8.67);
|
|
1179
|
+
expect(res.x).to.eql(5);
|
|
1180
|
+
expect(res.y).to.eql(5);
|
|
1181
|
+
expect(res.z).to.eql(5);
|
|
1182
|
+
});
|
|
1183
|
+
});
|
|
1184
|
+
|
|
1185
|
+
suite('when given a target vector', function() {
|
|
1186
|
+
test('should store limited vector in the target', function() {
|
|
1187
|
+
const target = new p5.Vector(0, 0, 0);
|
|
1188
|
+
p5.Vector.limit(v, 1, target);
|
|
1189
|
+
expect(target.x).to.be.closeTo(0.5773, 0.01);
|
|
1190
|
+
expect(target.y).to.be.closeTo(0.5773, 0.01);
|
|
1191
|
+
expect(target.z).to.be.closeTo(0.5773, 0.01);
|
|
1192
|
+
});
|
|
1193
|
+
});
|
|
1194
|
+
});
|
|
1195
|
+
});
|
|
1196
|
+
|
|
1197
|
+
suite('setMag', function() {
|
|
1198
|
+
let v;
|
|
1199
|
+
|
|
1200
|
+
setup(function() {
|
|
1201
|
+
v = new p5.Vector(1, 0, 0);
|
|
1202
|
+
});
|
|
1203
|
+
|
|
1204
|
+
suite('p5.Vector.setMag() [INSTANCE]', function() {
|
|
1205
|
+
test('should return the same object', function() {
|
|
1206
|
+
expect(v.setMag(2)).to.equal(v);
|
|
1207
|
+
});
|
|
1208
|
+
|
|
1209
|
+
test('should set the magnitude of the vector', function() {
|
|
1210
|
+
v.setMag(4);
|
|
1211
|
+
expect(v.x).to.eql(4);
|
|
1212
|
+
expect(v.y).to.eql(0);
|
|
1213
|
+
expect(v.z).to.eql(0);
|
|
1214
|
+
});
|
|
1215
|
+
|
|
1216
|
+
test('should set the magnitude of the vector', function() {
|
|
1217
|
+
v.x = 2;
|
|
1218
|
+
v.y = 3;
|
|
1219
|
+
v.z = -6;
|
|
1220
|
+
v.setMag(14);
|
|
1221
|
+
expect(v.x).to.eql(4);
|
|
1222
|
+
expect(v.y).to.eql(6);
|
|
1223
|
+
expect(v.z).to.eql(-12);
|
|
1224
|
+
});
|
|
1225
|
+
});
|
|
1226
|
+
|
|
1227
|
+
suite('p5.Vector.prototype.setMag() [CLASS]', function() {
|
|
1228
|
+
test('should not return the same object', function() {
|
|
1229
|
+
expect(p5.Vector.setMag(v, 2)).to.not.equal(v);
|
|
1230
|
+
});
|
|
1231
|
+
|
|
1232
|
+
test('should set the magnitude of the vector', function() {
|
|
1233
|
+
const res = p5.Vector.setMag(v, 4);
|
|
1234
|
+
expect(res.x).to.eql(4);
|
|
1235
|
+
expect(res.y).to.eql(0);
|
|
1236
|
+
expect(res.z).to.eql(0);
|
|
1237
|
+
});
|
|
1238
|
+
|
|
1239
|
+
test('should set the magnitude of the vector', function() {
|
|
1240
|
+
v.x = 2;
|
|
1241
|
+
v.y = 3;
|
|
1242
|
+
v.z = -6;
|
|
1243
|
+
const res = p5.Vector.setMag(v, 14);
|
|
1244
|
+
expect(res.x).to.eql(4);
|
|
1245
|
+
expect(res.y).to.eql(6);
|
|
1246
|
+
expect(res.z).to.eql(-12);
|
|
1247
|
+
});
|
|
1248
|
+
|
|
1249
|
+
suite('when given a target vector', function() {
|
|
1250
|
+
test('should set the magnitude on the target', function() {
|
|
1251
|
+
const target = new p5.Vector(0, 1, 0);
|
|
1252
|
+
const res = p5.Vector.setMag(v, 4, target);
|
|
1253
|
+
expect(target).to.equal(res);
|
|
1254
|
+
expect(target.x).to.eql(4);
|
|
1255
|
+
expect(target.y).to.eql(0);
|
|
1256
|
+
expect(target.z).to.eql(0);
|
|
1257
|
+
});
|
|
1258
|
+
});
|
|
1259
|
+
});
|
|
1260
|
+
});
|
|
1261
|
+
|
|
1262
|
+
suite('heading', function() {
|
|
1263
|
+
setup(function() {
|
|
1264
|
+
v = myp5.createVector();
|
|
1265
|
+
});
|
|
1266
|
+
|
|
1267
|
+
suite('p5.Vector.prototype.heading() [INSTANCE]', function() {
|
|
1268
|
+
test('should return a number', function() {
|
|
1269
|
+
expect(typeof v.heading() === 'number').to.eql(true);
|
|
1270
|
+
});
|
|
1271
|
+
|
|
1272
|
+
test('heading for vector pointing right is 0', function() {
|
|
1273
|
+
v.x = 1;
|
|
1274
|
+
v.y = 0;
|
|
1275
|
+
v.z = 0;
|
|
1276
|
+
expect(v.heading()).to.be.closeTo(0, 0.01);
|
|
1277
|
+
});
|
|
1278
|
+
|
|
1279
|
+
test('heading for vector pointing down is PI/2', function() {
|
|
1280
|
+
v.x = 0;
|
|
1281
|
+
v.y = 1;
|
|
1282
|
+
v.z = 0;
|
|
1283
|
+
expect(v.heading()).to.be.closeTo(Math.PI / 2, 0.01);
|
|
1284
|
+
});
|
|
1285
|
+
|
|
1286
|
+
test('heading for vector pointing left is PI', function() {
|
|
1287
|
+
v.x = -1;
|
|
1288
|
+
v.y = 0;
|
|
1289
|
+
v.z = 0;
|
|
1290
|
+
expect(v.heading()).to.be.closeTo(Math.PI, 0.01);
|
|
1291
|
+
});
|
|
1292
|
+
|
|
1293
|
+
suite('with `angleMode(DEGREES)`', function() {
|
|
1294
|
+
setup(function() {
|
|
1295
|
+
myp5.angleMode(DEGREES);
|
|
1296
|
+
});
|
|
1297
|
+
|
|
1298
|
+
test('heading for vector pointing right is 0', function() {
|
|
1299
|
+
v.x = 1;
|
|
1300
|
+
v.y = 0;
|
|
1301
|
+
v.z = 0;
|
|
1302
|
+
expect(v.heading()).to.equal(0);
|
|
1303
|
+
});
|
|
1304
|
+
|
|
1305
|
+
test('heading for vector pointing down is 90', function() {
|
|
1306
|
+
v.x = 0;
|
|
1307
|
+
v.y = 1;
|
|
1308
|
+
v.z = 0;
|
|
1309
|
+
expect(v.heading()).to.equal(90);
|
|
1310
|
+
});
|
|
1311
|
+
|
|
1312
|
+
test('heading for vector pointing left is 180', function() {
|
|
1313
|
+
v.x = -1;
|
|
1314
|
+
v.y = 0;
|
|
1315
|
+
v.z = 0;
|
|
1316
|
+
expect(v.heading()).to.equal(180);
|
|
1317
|
+
});
|
|
1318
|
+
});
|
|
1319
|
+
});
|
|
1320
|
+
|
|
1321
|
+
suite('p5.Vector.heading() [CLASS]', function() {
|
|
1322
|
+
test('should return a number', function() {
|
|
1323
|
+
expect(typeof p5.Vector.heading(v) === 'number').to.eql(true);
|
|
1324
|
+
});
|
|
1325
|
+
|
|
1326
|
+
test('heading for vector pointing right is 0', function() {
|
|
1327
|
+
v.x = 1;
|
|
1328
|
+
v.y = 0;
|
|
1329
|
+
v.z = 0;
|
|
1330
|
+
expect(p5.Vector.heading(v)).to.be.closeTo(0, 0.01);
|
|
1331
|
+
});
|
|
1332
|
+
|
|
1333
|
+
test('heading for vector pointing down is PI/2', function() {
|
|
1334
|
+
v.x = 0;
|
|
1335
|
+
v.y = 1;
|
|
1336
|
+
v.z = 0;
|
|
1337
|
+
expect(p5.Vector.heading(v)).to.be.closeTo(Math.PI / 2, 0.01);
|
|
1338
|
+
});
|
|
1339
|
+
|
|
1340
|
+
test('heading for vector pointing left is PI', function() {
|
|
1341
|
+
v.x = -1;
|
|
1342
|
+
v.y = 0;
|
|
1343
|
+
v.z = 0;
|
|
1344
|
+
expect(p5.Vector.heading(v)).to.be.closeTo(Math.PI, 0.01);
|
|
1345
|
+
});
|
|
1346
|
+
});
|
|
1347
|
+
});
|
|
1348
|
+
|
|
1349
|
+
suite('lerp', function() {
|
|
1350
|
+
test('should return the same object', function() {
|
|
1351
|
+
expect(v.lerp()).to.eql(v);
|
|
1352
|
+
});
|
|
1353
|
+
|
|
1354
|
+
// PEND: ADD BACK IN
|
|
1355
|
+
// suite('with p5.Vector', function() {
|
|
1356
|
+
// test('should call lerp with 4 arguments', function() {
|
|
1357
|
+
// spyOn(v, 'lerp').andCallThrough();
|
|
1358
|
+
// v.lerp(new p5.Vector(1,2,3), 1);
|
|
1359
|
+
// expect(v.lerp).toHaveBeenCalledWith(1, 2, 3, 1);
|
|
1360
|
+
// });
|
|
1361
|
+
// });
|
|
1362
|
+
|
|
1363
|
+
suite('with x, y, z, amt', function() {
|
|
1364
|
+
setup(function() {
|
|
1365
|
+
v.x = 0;
|
|
1366
|
+
v.y = 0;
|
|
1367
|
+
v.z = 0;
|
|
1368
|
+
v.lerp(2, 2, 2, 0.5);
|
|
1369
|
+
});
|
|
1370
|
+
|
|
1371
|
+
test('should lerp x by amt', function() {
|
|
1372
|
+
expect(v.x).to.eql(1);
|
|
1373
|
+
});
|
|
1374
|
+
|
|
1375
|
+
test('should lerp y by amt', function() {
|
|
1376
|
+
expect(v.y).to.eql(1);
|
|
1377
|
+
});
|
|
1378
|
+
|
|
1379
|
+
test('should lerp z by amt', function() {
|
|
1380
|
+
expect(v.z).to.eql(1);
|
|
1381
|
+
});
|
|
1382
|
+
});
|
|
1383
|
+
|
|
1384
|
+
suite('with no amt', function() {
|
|
1385
|
+
test('should assume 0 amt', function() {
|
|
1386
|
+
v.x = 0;
|
|
1387
|
+
v.y = 0;
|
|
1388
|
+
v.z = 0;
|
|
1389
|
+
v.lerp(2, 2, 2);
|
|
1390
|
+
expect(v.x).to.eql(0);
|
|
1391
|
+
expect(v.y).to.eql(0);
|
|
1392
|
+
expect(v.z).to.eql(0);
|
|
1393
|
+
});
|
|
1394
|
+
});
|
|
1395
|
+
});
|
|
1396
|
+
|
|
1397
|
+
suite('p5.Vector.lerp(v1, v2, amt)', function() {
|
|
1398
|
+
var res, v1, v2;
|
|
1399
|
+
setup(function() {
|
|
1400
|
+
v1 = new p5.Vector(0, 0, 0);
|
|
1401
|
+
v2 = new p5.Vector(2, 2, 2);
|
|
1402
|
+
res = p5.Vector.lerp(v1, v2, 0.5);
|
|
1403
|
+
});
|
|
1404
|
+
|
|
1405
|
+
test('should not be undefined', function() {
|
|
1406
|
+
expect(res).to.not.eql(undefined);
|
|
1407
|
+
});
|
|
1408
|
+
|
|
1409
|
+
test('should be a p5.Vector', function() {
|
|
1410
|
+
expect(res).to.be.an.instanceof(p5.Vector);
|
|
1411
|
+
});
|
|
1412
|
+
|
|
1413
|
+
test('should return neither v1 nor v2', function() {
|
|
1414
|
+
expect(res).to.not.eql(v1);
|
|
1415
|
+
expect(res).to.not.eql(v2);
|
|
1416
|
+
});
|
|
1417
|
+
|
|
1418
|
+
test('should res to be [1, 1, 1]', function() {
|
|
1419
|
+
expect(res.x).to.eql(1);
|
|
1420
|
+
expect(res.y).to.eql(1);
|
|
1421
|
+
expect(res.z).to.eql(1);
|
|
1422
|
+
});
|
|
1423
|
+
});
|
|
1424
|
+
|
|
1425
|
+
suite('v.slerp(w, amt)', function() {
|
|
1426
|
+
var w;
|
|
1427
|
+
setup(function() {
|
|
1428
|
+
v.set(1, 2, 3);
|
|
1429
|
+
w = new p5.Vector(4, 6, 8);
|
|
1430
|
+
});
|
|
1431
|
+
|
|
1432
|
+
test('if amt is 0, returns original vector', function() {
|
|
1433
|
+
v.slerp(w, 0);
|
|
1434
|
+
expect(v.x).to.eql(1);
|
|
1435
|
+
expect(v.y).to.eql(2);
|
|
1436
|
+
expect(v.z).to.eql(3);
|
|
1437
|
+
});
|
|
1438
|
+
|
|
1439
|
+
test('if amt is 1, returns argument vector', function() {
|
|
1440
|
+
v.slerp(w, 1);
|
|
1441
|
+
expect(v.x).to.eql(4);
|
|
1442
|
+
expect(v.y).to.eql(6);
|
|
1443
|
+
expect(v.z).to.eql(8);
|
|
1444
|
+
});
|
|
1445
|
+
|
|
1446
|
+
test('if both v and w are 2D, then result will also be 2D.', function() {
|
|
1447
|
+
v.set(2, 3, 0);
|
|
1448
|
+
w.set(3, -2, 0);
|
|
1449
|
+
v.slerp(w, 0.3);
|
|
1450
|
+
expect(v.z).to.eql(0);
|
|
1451
|
+
|
|
1452
|
+
v.set(1, 4, 0);
|
|
1453
|
+
w.set(-1, -4, 0);
|
|
1454
|
+
v.slerp(w, 0.8);
|
|
1455
|
+
expect(v.z).to.eql(0);
|
|
1456
|
+
});
|
|
1457
|
+
|
|
1458
|
+
test('if one side is a zero vector, linearly interpolate.', function() {
|
|
1459
|
+
v.set(0, 0, 0);
|
|
1460
|
+
w.set(2, 4, 6);
|
|
1461
|
+
v.slerp(w, 0.5);
|
|
1462
|
+
expect(v.x).to.eql(1);
|
|
1463
|
+
expect(v.y).to.eql(2);
|
|
1464
|
+
expect(v.z).to.eql(3);
|
|
1465
|
+
});
|
|
1466
|
+
|
|
1467
|
+
test('If they are pointing in the same direction, linearly interpolate.', function() {
|
|
1468
|
+
v.set(5, 11, 16);
|
|
1469
|
+
w.set(15, 33, 48);
|
|
1470
|
+
v.slerp(w, 0.5);
|
|
1471
|
+
expect(v.x).to.eql(10);
|
|
1472
|
+
expect(v.y).to.eql(22);
|
|
1473
|
+
expect(v.z).to.eql(32);
|
|
1474
|
+
});
|
|
1475
|
+
});
|
|
1476
|
+
|
|
1477
|
+
suite('p5.Vector.slerp(v1, v2, amt)', function() {
|
|
1478
|
+
var res, v1, v2;
|
|
1479
|
+
setup(function() {
|
|
1480
|
+
v1 = new p5.Vector(1, 0, 0);
|
|
1481
|
+
v2 = new p5.Vector(0, 0, 1);
|
|
1482
|
+
res = p5.Vector.slerp(v1, v2, 1/3);
|
|
1483
|
+
});
|
|
1484
|
+
|
|
1485
|
+
test('should not be undefined', function() {
|
|
1486
|
+
expect(res).to.not.eql(undefined);
|
|
1487
|
+
});
|
|
1488
|
+
|
|
1489
|
+
test('should be a p5.Vector', function() {
|
|
1490
|
+
expect(res).to.be.an.instanceof(p5.Vector);
|
|
1491
|
+
});
|
|
1492
|
+
|
|
1493
|
+
test('should return neither v1 nor v2', function() {
|
|
1494
|
+
expect(res).to.not.eql(v1);
|
|
1495
|
+
expect(res).to.not.eql(v2);
|
|
1496
|
+
});
|
|
1497
|
+
|
|
1498
|
+
test('Make sure the interpolation in 1/3 is correct', function() {
|
|
1499
|
+
expect(res.x).to.be.closeTo(Math.cos(Math.PI/6), 0.00001);
|
|
1500
|
+
expect(res.y).to.be.closeTo(0, 0.00001);
|
|
1501
|
+
expect(res.z).to.be.closeTo(Math.sin(Math.PI/6), 0.00001);
|
|
1502
|
+
});
|
|
1503
|
+
|
|
1504
|
+
test('Make sure the interpolation in -1/3 is correct', function() {
|
|
1505
|
+
p5.Vector.slerp(v1, v2, -1/3, res);
|
|
1506
|
+
expect(res.x).to.be.closeTo(Math.cos(-Math.PI/6), 0.00001);
|
|
1507
|
+
expect(res.y).to.be.closeTo(0, 0.00001);
|
|
1508
|
+
expect(res.z).to.be.closeTo(Math.sin(-Math.PI/6), 0.00001);
|
|
1509
|
+
});
|
|
1510
|
+
|
|
1511
|
+
test('Make sure the interpolation in 5/3 is correct', function() {
|
|
1512
|
+
p5.Vector.slerp(v1, v2, 5/3, res);
|
|
1513
|
+
expect(res.x).to.be.closeTo(Math.cos(5*Math.PI/6), 0.00001);
|
|
1514
|
+
expect(res.y).to.be.closeTo(0, 0.00001);
|
|
1515
|
+
expect(res.z).to.be.closeTo(Math.sin(5*Math.PI/6), 0.00001);
|
|
1516
|
+
});
|
|
1517
|
+
});
|
|
1518
|
+
|
|
1519
|
+
suite('p5.Vector.fromAngle(angle)', function() {
|
|
1520
|
+
var res, angle;
|
|
1521
|
+
setup(function() {
|
|
1522
|
+
angle = Math.PI / 2;
|
|
1523
|
+
res = p5.Vector.fromAngle(angle);
|
|
1524
|
+
});
|
|
1525
|
+
|
|
1526
|
+
test('should be a p5.Vector with values (0,1)', function() {
|
|
1527
|
+
expect(res.x).to.be.closeTo(0, 0.01);
|
|
1528
|
+
expect(res.y).to.be.closeTo(1, 0.01);
|
|
1529
|
+
});
|
|
1530
|
+
});
|
|
1531
|
+
|
|
1532
|
+
suite('p5.Vector.random2D()', function() {
|
|
1533
|
+
var res;
|
|
1534
|
+
setup(function() {
|
|
1535
|
+
res = p5.Vector.random2D();
|
|
1536
|
+
});
|
|
1537
|
+
|
|
1538
|
+
test('should be a unit p5.Vector', function() {
|
|
1539
|
+
expect(res.mag()).to.be.closeTo(1, 0.01);
|
|
1540
|
+
});
|
|
1541
|
+
});
|
|
1542
|
+
|
|
1543
|
+
suite('p5.Vector.random3D()', function() {
|
|
1544
|
+
var res;
|
|
1545
|
+
setup(function() {
|
|
1546
|
+
res = p5.Vector.random3D();
|
|
1547
|
+
});
|
|
1548
|
+
test('should be a unit p5.Vector', function() {
|
|
1549
|
+
expect(res.mag()).to.be.closeTo(1, 0.01);
|
|
1550
|
+
});
|
|
1551
|
+
});
|
|
1552
|
+
|
|
1553
|
+
suite('array', function() {
|
|
1554
|
+
setup(function() {
|
|
1555
|
+
v = new p5.Vector(1, 23, 4);
|
|
1556
|
+
});
|
|
1557
|
+
|
|
1558
|
+
suite('p5.Vector.prototype.array() [INSTANCE]', function() {
|
|
1559
|
+
test('should return an array', function() {
|
|
1560
|
+
expect(v.array()).to.be.instanceof(Array);
|
|
1561
|
+
});
|
|
1562
|
+
|
|
1563
|
+
test('should return an with the x y and z components', function() {
|
|
1564
|
+
expect(v.array()).to.eql([1, 23, 4]);
|
|
1565
|
+
});
|
|
1566
|
+
});
|
|
1567
|
+
|
|
1568
|
+
suite('p5.Vector.array() [CLASS]', function() {
|
|
1569
|
+
test('should return an array', function() {
|
|
1570
|
+
expect(p5.Vector.array(v)).to.be.instanceof(Array);
|
|
1571
|
+
});
|
|
1572
|
+
|
|
1573
|
+
test('should return an with the x y and z components', function() {
|
|
1574
|
+
expect(p5.Vector.array(v)).to.eql([1, 23, 4]);
|
|
1575
|
+
});
|
|
1576
|
+
});
|
|
1577
|
+
});
|
|
1578
|
+
|
|
1579
|
+
suite('reflect', function() {
|
|
1580
|
+
suite('p5.Vector.prototype.reflect() [INSTANCE]', function() {
|
|
1581
|
+
setup(function() {
|
|
1582
|
+
incoming_x = 1;
|
|
1583
|
+
incoming_y = 1;
|
|
1584
|
+
incoming_z = 1;
|
|
1585
|
+
original_incoming = new p5.Vector(incoming_x, incoming_y, incoming_z);
|
|
1586
|
+
|
|
1587
|
+
x_normal = new p5.Vector(3, 0, 0);
|
|
1588
|
+
y_normal = new p5.Vector(0, 3, 0);
|
|
1589
|
+
z_normal = new p5.Vector(0, 0, 3);
|
|
1590
|
+
|
|
1591
|
+
x_bounce_incoming = new p5.Vector(incoming_x, incoming_y, incoming_z);
|
|
1592
|
+
x_bounce_outgoing = x_bounce_incoming.reflect(x_normal);
|
|
1593
|
+
|
|
1594
|
+
y_bounce_incoming = new p5.Vector(incoming_x, incoming_y, incoming_z);
|
|
1595
|
+
y_bounce_outgoing = y_bounce_incoming.reflect(y_normal);
|
|
1596
|
+
|
|
1597
|
+
z_bounce_incoming = new p5.Vector(incoming_x, incoming_y, incoming_z);
|
|
1598
|
+
z_bounce_outgoing = z_bounce_incoming.reflect(z_normal);
|
|
1599
|
+
});
|
|
1600
|
+
|
|
1601
|
+
test('should return a p5.Vector', function() {
|
|
1602
|
+
expect(x_bounce_incoming).to.be.an.instanceof(p5.Vector);
|
|
1603
|
+
expect(y_bounce_incoming).to.be.an.instanceof(p5.Vector);
|
|
1604
|
+
expect(z_bounce_incoming).to.be.an.instanceof(p5.Vector);
|
|
1605
|
+
});
|
|
1606
|
+
|
|
1607
|
+
test('should update this', function() {
|
|
1608
|
+
assert.equal(x_bounce_incoming, x_bounce_outgoing);
|
|
1609
|
+
assert.equal(y_bounce_incoming, y_bounce_outgoing);
|
|
1610
|
+
assert.equal(z_bounce_incoming, z_bounce_outgoing);
|
|
1611
|
+
});
|
|
1612
|
+
|
|
1613
|
+
test('x-normal should flip incoming x component and maintain y,z components', function() {
|
|
1614
|
+
expect(x_bounce_outgoing.x).to.be.closeTo(-1, 0.01);
|
|
1615
|
+
expect(x_bounce_outgoing.y).to.be.closeTo(1, 0.01);
|
|
1616
|
+
expect(x_bounce_outgoing.z).to.be.closeTo(1, 0.01);
|
|
1617
|
+
});
|
|
1618
|
+
test('y-normal should flip incoming y component and maintain x,z components', function() {
|
|
1619
|
+
expect(y_bounce_outgoing.x).to.be.closeTo(1, 0.01);
|
|
1620
|
+
expect(y_bounce_outgoing.y).to.be.closeTo(-1, 0.01);
|
|
1621
|
+
expect(y_bounce_outgoing.z).to.be.closeTo(1, 0.01);
|
|
1622
|
+
});
|
|
1623
|
+
test('z-normal should flip incoming z component and maintain x,y components', function() {
|
|
1624
|
+
expect(z_bounce_outgoing.x).to.be.closeTo(1, 0.01);
|
|
1625
|
+
expect(z_bounce_outgoing.y).to.be.closeTo(1, 0.01);
|
|
1626
|
+
expect(z_bounce_outgoing.z).to.be.closeTo(-1, 0.01);
|
|
1627
|
+
});
|
|
1628
|
+
|
|
1629
|
+
test('angle of incidence should match angle of reflection', function() {
|
|
1630
|
+
expect(
|
|
1631
|
+
Math.abs(x_normal.angleBetween(original_incoming))
|
|
1632
|
+
).to.be.closeTo(
|
|
1633
|
+
Math.abs(x_normal.angleBetween(x_bounce_outgoing.mult(-1))),
|
|
1634
|
+
0.01
|
|
1635
|
+
);
|
|
1636
|
+
expect(
|
|
1637
|
+
Math.abs(y_normal.angleBetween(original_incoming))
|
|
1638
|
+
).to.be.closeTo(
|
|
1639
|
+
Math.abs(y_normal.angleBetween(y_bounce_outgoing.mult(-1))),
|
|
1640
|
+
0.01
|
|
1641
|
+
);
|
|
1642
|
+
expect(
|
|
1643
|
+
Math.abs(z_normal.angleBetween(original_incoming))
|
|
1644
|
+
).to.be.closeTo(
|
|
1645
|
+
Math.abs(z_normal.angleBetween(z_bounce_outgoing.mult(-1))),
|
|
1646
|
+
0.01
|
|
1647
|
+
);
|
|
1648
|
+
});
|
|
1649
|
+
test('should not update surface normal', function() {
|
|
1650
|
+
const tolerance = 0.001;
|
|
1651
|
+
assert.closeTo(x_normal.x, 3, tolerance);
|
|
1652
|
+
assert.closeTo(x_normal.y, 0, tolerance);
|
|
1653
|
+
assert.closeTo(x_normal.z, 0, tolerance);
|
|
1654
|
+
|
|
1655
|
+
assert.closeTo(y_normal.x, 0, tolerance);
|
|
1656
|
+
assert.closeTo(y_normal.y, 3, tolerance);
|
|
1657
|
+
assert.closeTo(y_normal.z, 0, tolerance);
|
|
1658
|
+
|
|
1659
|
+
assert.closeTo(z_normal.x, 0, tolerance);
|
|
1660
|
+
assert.closeTo(z_normal.y, 0, tolerance);
|
|
1661
|
+
assert.closeTo(z_normal.z, 3, tolerance);
|
|
1662
|
+
});
|
|
1663
|
+
|
|
1664
|
+
});
|
|
1665
|
+
|
|
1666
|
+
suite('p5.Vector.reflect() [CLASS]', function() {
|
|
1667
|
+
setup(function() {
|
|
1668
|
+
incoming_x = 1;
|
|
1669
|
+
incoming_y = 1;
|
|
1670
|
+
incoming_z = 1;
|
|
1671
|
+
original_incoming = new p5.Vector(incoming_x, incoming_y, incoming_z);
|
|
1672
|
+
x_target = new p5.Vector();
|
|
1673
|
+
y_target = new p5.Vector();
|
|
1674
|
+
z_target = new p5.Vector();
|
|
1675
|
+
|
|
1676
|
+
x_normal = new p5.Vector(3, 0, 0);
|
|
1677
|
+
y_normal = new p5.Vector(0, 3, 0);
|
|
1678
|
+
z_normal = new p5.Vector(0, 0, 3);
|
|
1679
|
+
|
|
1680
|
+
x_bounce_incoming = new p5.Vector(incoming_x, incoming_y, incoming_z);
|
|
1681
|
+
x_bounce_outgoing = p5.Vector.reflect(
|
|
1682
|
+
x_bounce_incoming,
|
|
1683
|
+
x_normal,
|
|
1684
|
+
x_target
|
|
1685
|
+
);
|
|
1686
|
+
|
|
1687
|
+
y_bounce_incoming = new p5.Vector(incoming_x, incoming_y, incoming_z);
|
|
1688
|
+
y_bounce_outgoing = p5.Vector.reflect(
|
|
1689
|
+
y_bounce_incoming,
|
|
1690
|
+
y_normal,
|
|
1691
|
+
y_target
|
|
1692
|
+
);
|
|
1693
|
+
|
|
1694
|
+
z_bounce_incoming = new p5.Vector(incoming_x, incoming_y, incoming_z);
|
|
1695
|
+
z_bounce_outgoing = p5.Vector.reflect(
|
|
1696
|
+
z_bounce_incoming,
|
|
1697
|
+
z_normal,
|
|
1698
|
+
z_target
|
|
1699
|
+
);
|
|
1700
|
+
});
|
|
1701
|
+
|
|
1702
|
+
test('should return a p5.Vector', function() {
|
|
1703
|
+
expect(x_bounce_incoming).to.be.an.instanceof(p5.Vector);
|
|
1704
|
+
expect(y_bounce_incoming).to.be.an.instanceof(p5.Vector);
|
|
1705
|
+
expect(z_bounce_incoming).to.be.an.instanceof(p5.Vector);
|
|
1706
|
+
});
|
|
1707
|
+
|
|
1708
|
+
test('should not update this', function() {
|
|
1709
|
+
expect(x_bounce_incoming).to.not.equal(x_bounce_outgoing);
|
|
1710
|
+
expect(y_bounce_incoming).to.not.equal(y_bounce_outgoing);
|
|
1711
|
+
expect(z_bounce_incoming).to.not.equal(z_bounce_outgoing);
|
|
1712
|
+
});
|
|
1713
|
+
|
|
1714
|
+
test('should not update surface normal', function() {
|
|
1715
|
+
const tolerance = 0.001;
|
|
1716
|
+
assert.closeTo(x_normal.x, 3, tolerance);
|
|
1717
|
+
assert.closeTo(x_normal.y, 0, tolerance);
|
|
1718
|
+
assert.closeTo(x_normal.z, 0, tolerance);
|
|
1719
|
+
|
|
1720
|
+
assert.closeTo(y_normal.x, 0, tolerance);
|
|
1721
|
+
assert.closeTo(y_normal.y, 3, tolerance);
|
|
1722
|
+
assert.closeTo(y_normal.z, 0, tolerance);
|
|
1723
|
+
|
|
1724
|
+
assert.closeTo(z_normal.x, 0, tolerance);
|
|
1725
|
+
assert.closeTo(z_normal.y, 0, tolerance);
|
|
1726
|
+
assert.closeTo(z_normal.z, 3, tolerance);
|
|
1727
|
+
});
|
|
1728
|
+
|
|
1729
|
+
|
|
1730
|
+
test('should update target', function() {
|
|
1731
|
+
assert.equal(x_target, x_bounce_outgoing);
|
|
1732
|
+
assert.equal(y_target, y_bounce_outgoing);
|
|
1733
|
+
assert.equal(z_target, z_bounce_outgoing);
|
|
1734
|
+
});
|
|
1735
|
+
|
|
1736
|
+
test('x-normal should flip incoming x component and maintain y,z components', function() {
|
|
1737
|
+
expect(x_bounce_outgoing.x).to.be.closeTo(-1, 0.01);
|
|
1738
|
+
expect(x_bounce_outgoing.y).to.be.closeTo(1, 0.01);
|
|
1739
|
+
expect(x_bounce_outgoing.z).to.be.closeTo(1, 0.01);
|
|
1740
|
+
});
|
|
1741
|
+
test('y-normal should flip incoming y component and maintain x,z components', function() {
|
|
1742
|
+
expect(y_bounce_outgoing.x).to.be.closeTo(1, 0.01);
|
|
1743
|
+
expect(y_bounce_outgoing.y).to.be.closeTo(-1, 0.01);
|
|
1744
|
+
expect(y_bounce_outgoing.z).to.be.closeTo(1, 0.01);
|
|
1745
|
+
});
|
|
1746
|
+
test('z-normal should flip incoming z component and maintain x,y components', function() {
|
|
1747
|
+
expect(z_bounce_outgoing.x).to.be.closeTo(1, 0.01);
|
|
1748
|
+
expect(z_bounce_outgoing.y).to.be.closeTo(1, 0.01);
|
|
1749
|
+
expect(z_bounce_outgoing.z).to.be.closeTo(-1, 0.01);
|
|
1750
|
+
});
|
|
1751
|
+
|
|
1752
|
+
test('angle of incidence should match angle of reflection', function() {
|
|
1753
|
+
expect(
|
|
1754
|
+
Math.abs(x_normal.angleBetween(original_incoming))
|
|
1755
|
+
).to.be.closeTo(
|
|
1756
|
+
Math.abs(x_normal.angleBetween(x_bounce_outgoing.mult(-1))),
|
|
1757
|
+
0.01
|
|
1758
|
+
);
|
|
1759
|
+
expect(
|
|
1760
|
+
Math.abs(y_normal.angleBetween(original_incoming))
|
|
1761
|
+
).to.be.closeTo(
|
|
1762
|
+
Math.abs(y_normal.angleBetween(y_bounce_outgoing.mult(-1))),
|
|
1763
|
+
0.01
|
|
1764
|
+
);
|
|
1765
|
+
expect(
|
|
1766
|
+
Math.abs(z_normal.angleBetween(original_incoming))
|
|
1767
|
+
).to.be.closeTo(
|
|
1768
|
+
Math.abs(z_normal.angleBetween(z_bounce_outgoing.mult(-1))),
|
|
1769
|
+
0.01
|
|
1770
|
+
);
|
|
1771
|
+
});
|
|
1772
|
+
});
|
|
1773
|
+
});
|
|
1774
|
+
|
|
1775
|
+
suite('mag', function() {
|
|
1776
|
+
const MAG = 3.7416573867739413; // sqrt(1*1 + 2*2 + 3*3)
|
|
1777
|
+
|
|
1778
|
+
let v0;
|
|
1779
|
+
let v1;
|
|
1780
|
+
|
|
1781
|
+
setup(function() {
|
|
1782
|
+
v0 = new p5.Vector(0, 0, 0);
|
|
1783
|
+
v1 = new p5.Vector(1, 2, 3);
|
|
1784
|
+
});
|
|
1785
|
+
|
|
1786
|
+
suite('p5.Vector.prototype.mag() [INSTANCE]', function() {
|
|
1787
|
+
test('should return the magnitude of the vector', function() {
|
|
1788
|
+
expect(v0.mag()).to.eql(0);
|
|
1789
|
+
expect(v1.mag()).to.eql(MAG);
|
|
1790
|
+
});
|
|
1791
|
+
});
|
|
1792
|
+
|
|
1793
|
+
suite('p5.Vector.mag() [CLASS]', function() {
|
|
1794
|
+
test('should return the magnitude of the vector', function() {
|
|
1795
|
+
expect(p5.Vector.mag(v0)).to.eql(0);
|
|
1796
|
+
expect(p5.Vector.mag(v1)).to.eql(MAG);
|
|
1797
|
+
});
|
|
1798
|
+
});
|
|
1799
|
+
});
|
|
1800
|
+
|
|
1801
|
+
suite('magSq', function() {
|
|
1802
|
+
const MAG = 14; // 1*1 + 2*2 + 3*3
|
|
1803
|
+
|
|
1804
|
+
let v0;
|
|
1805
|
+
let v1;
|
|
1806
|
+
|
|
1807
|
+
setup(function() {
|
|
1808
|
+
v0 = new p5.Vector(0, 0, 0);
|
|
1809
|
+
v1 = new p5.Vector(1, 2, 3);
|
|
1810
|
+
});
|
|
1811
|
+
|
|
1812
|
+
suite('p5.Vector.prototype.magSq() [INSTANCE]', function() {
|
|
1813
|
+
test('should return the magnitude of the vector', function() {
|
|
1814
|
+
expect(v0.magSq()).to.eql(0);
|
|
1815
|
+
expect(v1.magSq()).to.eql(MAG);
|
|
1816
|
+
});
|
|
1817
|
+
});
|
|
1818
|
+
|
|
1819
|
+
suite('p5.Vector.magSq() [CLASS]', function() {
|
|
1820
|
+
test('should return the magnitude of the vector', function() {
|
|
1821
|
+
expect(p5.Vector.magSq(v0)).to.eql(0);
|
|
1822
|
+
expect(p5.Vector.magSq(v1)).to.eql(MAG);
|
|
1823
|
+
});
|
|
1824
|
+
});
|
|
1825
|
+
});
|
|
1826
|
+
|
|
1827
|
+
suite('equals', function() {
|
|
1828
|
+
suite('p5.Vector.prototype.equals() [INSTANCE]', function() {
|
|
1829
|
+
test('should return false for parameters inequal to the vector', function() {
|
|
1830
|
+
const v1 = new p5.Vector(0, -1, 1);
|
|
1831
|
+
const v2 = new p5.Vector(1, 2, 3);
|
|
1832
|
+
const a2 = [1, 2, 3];
|
|
1833
|
+
expect(v1.equals(v2)).to.be.false;
|
|
1834
|
+
expect(v1.equals(a2)).to.be.false;
|
|
1835
|
+
expect(v1.equals(1, 2, 3)).to.be.false;
|
|
1836
|
+
});
|
|
1837
|
+
|
|
1838
|
+
test('should return true for equal vectors', function() {
|
|
1839
|
+
const v1 = new p5.Vector(0, -1, 1);
|
|
1840
|
+
const v2 = new p5.Vector(0, -1, 1);
|
|
1841
|
+
expect(v1.equals(v2)).to.be.true;
|
|
1842
|
+
});
|
|
1843
|
+
|
|
1844
|
+
test('should return true for arrays equal to the vector', function() {
|
|
1845
|
+
const v1 = new p5.Vector(0, -1, 1);
|
|
1846
|
+
const a1 = [0, -1, 1];
|
|
1847
|
+
expect(v1.equals(a1)).to.be.true;
|
|
1848
|
+
});
|
|
1849
|
+
|
|
1850
|
+
test('should return true for arguments equal to the vector', function() {
|
|
1851
|
+
const v1 = new p5.Vector(0, -1, 1);
|
|
1852
|
+
expect(v1.equals(0, -1, 1)).to.be.true;
|
|
1853
|
+
});
|
|
1854
|
+
});
|
|
1855
|
+
|
|
1856
|
+
suite('p5.Vector.equals() [CLASS]', function() {
|
|
1857
|
+
test('should return false for inequal parameters', function() {
|
|
1858
|
+
const v1 = new p5.Vector(0, -1, 1);
|
|
1859
|
+
const v2 = new p5.Vector(1, 2, 3);
|
|
1860
|
+
const a2 = [1, 2, 3];
|
|
1861
|
+
expect(p5.Vector.equals(v1, v2)).to.be.false;
|
|
1862
|
+
expect(p5.Vector.equals(v1, a2)).to.be.false;
|
|
1863
|
+
expect(p5.Vector.equals(a2, v1)).to.be.false;
|
|
1864
|
+
});
|
|
1865
|
+
|
|
1866
|
+
test('should return true for equal vectors', function() {
|
|
1867
|
+
const v1 = new p5.Vector(0, -1, 1);
|
|
1868
|
+
const v2 = new p5.Vector(0, -1, 1);
|
|
1869
|
+
expect(p5.Vector.equals(v1, v2)).to.be.true;
|
|
1870
|
+
});
|
|
1871
|
+
|
|
1872
|
+
test('should return true for equal vectors and arrays', function() {
|
|
1873
|
+
const v1 = new p5.Vector(0, -1, 1);
|
|
1874
|
+
const a1 = [0, -1, 1];
|
|
1875
|
+
expect(p5.Vector.equals(v1, a1)).to.be.true;
|
|
1876
|
+
expect(p5.Vector.equals(a1, v1)).to.be.true;
|
|
1877
|
+
});
|
|
1878
|
+
|
|
1879
|
+
test('should return true for equal arrays', function() {
|
|
1880
|
+
const a1 = [0, -1, 1];
|
|
1881
|
+
const a2 = [0, -1, 1];
|
|
1882
|
+
expect(p5.Vector.equals(a1, a2)).to.be.true;
|
|
1883
|
+
});
|
|
1884
|
+
});
|
|
1885
|
+
});
|
|
1886
|
+
});
|