Skip to content

Commit 92fe4cd

Browse files
committed
Added PVector.sub() perf was 311ms now is 190ms! Remove sub PVector unit tests from PVector.pde to PVector-sub.pde and added a whole bunch more unit tests for sub.
1 parent 29c33f5 commit 92fe4cd

File tree

3 files changed

+125
-13
lines changed

3 files changed

+125
-13
lines changed

processing.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,10 @@
11521152
return v1.cross(v2);
11531153
};
11541154

1155+
PVector.sub = function(v1, v2) {
1156+
return new PVector(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
1157+
};
1158+
11551159
PVector.angleBetween = function(v1, v2) {
11561160
return Math.acos(v1.dot(v2) / (v1.mag() * v2.mag()));
11571161
};

test/unit/PVector-sub.pde

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// SETUP - Subtract using class method 2D
2+
PVector v1 = new PVector(10, 20);
3+
PVector v2 = new PVector(40, 50);
4+
5+
PVector res = PVector.sub(v1, v2);
6+
_checkEqual([res.x, res.y], [-30, -30]);
7+
8+
9+
10+
// SETUP - Subtract using class method 2D
11+
v1 = new PVector(-5, 90);
12+
v2 = new PVector(40, -20);
13+
14+
res = PVector.sub(v2, v1);
15+
_checkEqual([res.x, res.y], [45, -110]);
16+
17+
18+
19+
// SETUP - Subtract using instance method 2D
20+
v1 = new PVector(1, 2, 3);
21+
v2 = new PVector(10, 20, 30);
22+
23+
v1.sub(v2);
24+
_checkEqual([v1.x, v1.y, v1.z], [-9, -18, -27]);
25+
26+
27+
28+
// SETUP - Subtract using instance method 2D
29+
v1 = new PVector(1, 2);
30+
v2 = new PVector(10, 20);
31+
32+
v2.sub(v1);
33+
_checkEqual([v2.x, v2.y], [9, 18]);
34+
35+
36+
37+
// SETUP - Subtract using class method 3D
38+
v1 = new PVector(10, 20, 30);
39+
v2 = new PVector(40, 50, 60);
40+
41+
PVector res = PVector.sub(v1, v2);
42+
_checkEqual([res.x, res.y, res.z], [-30, -30, -30]);
43+
44+
45+
46+
// SETUP - Subtract using class method 3D
47+
v1 = new PVector(-15, 30, 45);
48+
v2 = new PVector(70, -40, -60);
49+
50+
res = PVector.sub(v2, v1);
51+
_checkEqual([res.x, res.y, res.z], [85, -70, -105]);
52+
53+
54+
55+
// SETUP - Subtract using class method with different vector dimensions
56+
v1 = new PVector(10, 20);
57+
v2 = new PVector(40, 50, 60);
58+
59+
PVector res = PVector.sub(v1, v2);
60+
_checkEqual([res.x, res.y, res.z], [-30, -30, -60]);
61+
62+
63+
64+
// SETUP - Subtract using class method with different vector dimensions
65+
v1 = new PVector(-5, 90, 20);
66+
v2 = new PVector(40, -20);
67+
68+
res = PVector.sub(v2, v1);
69+
_checkEqual([res.x, res.y, res.z], [45, -110, -20]);
70+
71+
72+
73+
// SETUP - Subtract using instance method 3D
74+
v1 = new PVector(1, 2, 3);
75+
v2 = new PVector(10, 20, 30);
76+
77+
v1.sub(v2);
78+
_checkEqual([v1.x, v1.y, v1.z], [-9, -18, -27]);
79+
80+
81+
82+
// SETUP - Subtract using instance method 3D
83+
v1 = new PVector(1, 2, 3);
84+
v2 = new PVector(10, 20, 30);
85+
86+
v2.sub(v1);
87+
_checkEqual([v2.x, v2.y, v2.z], [9, 18, 27]);
88+
89+
90+
91+
// SETUP - Subtract using instance method with x,y,z
92+
v1 = new PVector(1, 2, 3);
93+
94+
v1.sub(10, 20, 30);
95+
_checkEqual([v1.x, v1.y, v1.z], [-9, -18, -27]);
96+
97+
98+
99+
// SETUP - Subtract using instance method with x,y,z
100+
v1 = new PVector(10, 20, 30);
101+
102+
v1.sub(1, 2, 3);
103+
_checkEqual([v2.x, v2.y, v2.z], [9, 18, 27]);
104+
105+
106+
107+
// SETUP - Subtract vectors of different dimensions
108+
v1 = new PVector(1, 2, 3);
109+
v2 = new PVector(10, 20);
110+
111+
v1.sub(v2);
112+
_checkEqual([v1.x, v1.y, v1.z], [-9, -18, 3]);
113+
114+
115+
116+
// SETUP - Subtract vectors of different dimensions
117+
v1 = new PVector(5, 10);
118+
v2 = new PVector(5, 10, 15);
119+
120+
v1.sub(v2);
121+
_checkEqual([v1.x, v1.y, v1.z], [0, 0, -15]);

test/unit/PVector.pde

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,6 @@ _checkEqual(v3.z, 0);
6969

7070
PVector v4 = new PVector(10, 10, 10);
7171

72-
// SUB
73-
v3.sub(v4);
74-
75-
_checkEqual(v3.x, 95);
76-
_checkEqual(v3.y, 80);
77-
_checkEqual(v3.z, -10);
78-
79-
PVector v5 = PVector.sub(v3,v4);
80-
81-
_checkEqual(v5.x, 85);
82-
_checkEqual(v5.y, 70);
83-
_checkEqual(v5.z, -20);
84-
8572
PVector v1, v2;
8673
v1 = new PVector(40, 20, 15);
8774
v2 = new PVector(25, 50, 10);

0 commit comments

Comments
 (0)