Skip to content

Commit caa29fe

Browse files
authored
Merge pull request #5469 from limzykenneth/vector-cyclic-reference
Remove reference to p5 instance in p5.Vector objects
2 parents 8fddc40 + 261d7b3 commit caa29fe

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

src/math/math.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ import p5 from '../core/main';
3838
*/
3939
p5.prototype.createVector = function(x, y, z) {
4040
if (this instanceof p5) {
41-
return new p5.Vector(this, arguments);
41+
return new p5.Vector(
42+
this._fromRadians.bind(this),
43+
this._toRadians.bind(this),
44+
...arguments
45+
);
4246
} else {
4347
return new p5.Vector(x, y, z);
4448
}

src/math/p5.Vector.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,17 @@ import * as constants from '../core/constants';
4949
*/
5050
p5.Vector = function Vector() {
5151
let x, y, z;
52+
5253
// This is how it comes in with createVector()
53-
if (arguments[0] instanceof p5) {
54-
// save reference to p5 if passed in
55-
this.p5 = arguments[0];
56-
x = arguments[1][0] || 0;
57-
y = arguments[1][1] || 0;
58-
z = arguments[1][2] || 0;
54+
// This check if the first argument is a function
55+
if ({}.toString.call(arguments[0]) === '[object Function]') {
56+
// In this case the vector have an associated p5 instance
57+
this.isPInst = true;
58+
this._fromRadians = arguments[0];
59+
this._toRadians = arguments[1];
60+
x = arguments[2] || 0;
61+
y = arguments[3] || 0;
62+
z = arguments[4] || 0;
5963
// This is what we'll get with new p5.Vector()
6064
} else {
6165
x = arguments[0] || 0;
@@ -228,8 +232,14 @@ p5.Vector.prototype.set = function set(x, y, z) {
228232
* </div>
229233
*/
230234
p5.Vector.prototype.copy = function copy() {
231-
if (this.p5) {
232-
return new p5.Vector(this.p5, [this.x, this.y, this.z]);
235+
if (this.isPInst) {
236+
return new p5.Vector(
237+
this._fromRadians,
238+
this._toRadians,
239+
this.x,
240+
this.y,
241+
this.z
242+
);
233243
} else {
234244
return new p5.Vector(this.x, this.y, this.z);
235245
}
@@ -1125,8 +1135,8 @@ p5.Vector.prototype.cross = function cross(v) {
11251135
const x = this.y * v.z - this.z * v.y;
11261136
const y = this.z * v.x - this.x * v.z;
11271137
const z = this.x * v.y - this.y * v.x;
1128-
if (this.p5) {
1129-
return new p5.Vector(this.p5, [x, y, z]);
1138+
if (this.isPInst) {
1139+
return new p5.Vector(this._fromRadians, this._toRadians, x, y, z);
11301140
} else {
11311141
return new p5.Vector(x, y, z);
11321142
}
@@ -1456,7 +1466,7 @@ p5.Vector.prototype.setMag = function setMag(n) {
14561466
*/
14571467
p5.Vector.prototype.heading = function heading() {
14581468
const h = Math.atan2(this.y, this.x);
1459-
if (this.p5) return this.p5._fromRadians(h);
1469+
if (this.isPInst) return this._fromRadians(h);
14601470
return h;
14611471
};
14621472

@@ -1547,7 +1557,7 @@ p5.Vector.prototype.setHeading = function setHeading(a) {
15471557
*/
15481558
p5.Vector.prototype.rotate = function rotate(a) {
15491559
let newHeading = this.heading() + a;
1550-
if (this.p5) newHeading = this.p5._toRadians(newHeading);
1560+
if (this.isPInst) newHeading = this._toRadians(newHeading);
15511561
const mag = this.mag();
15521562
this.x = Math.cos(newHeading) * mag;
15531563
this.y = Math.sin(newHeading) * mag;
@@ -1629,8 +1639,8 @@ p5.Vector.prototype.angleBetween = function angleBetween(v) {
16291639
let angle;
16301640
angle = Math.acos(Math.min(1, Math.max(-1, dotmagmag)));
16311641
angle = angle * Math.sign(this.cross(v).z || 1);
1632-
if (this.p5) {
1633-
angle = this.p5._fromRadians(angle);
1642+
if (this.isPInst) {
1643+
angle = this._fromRadians(angle);
16341644
}
16351645
return angle;
16361646
};

0 commit comments

Comments
 (0)