@@ -49,13 +49,17 @@ import * as constants from '../core/constants';
49
49
*/
50
50
p5 . Vector = function Vector ( ) {
51
51
let x , y , z ;
52
+
52
53
// 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 ;
59
63
// This is what we'll get with new p5.Vector()
60
64
} else {
61
65
x = arguments [ 0 ] || 0 ;
@@ -228,8 +232,14 @@ p5.Vector.prototype.set = function set(x, y, z) {
228
232
* </div>
229
233
*/
230
234
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
+ ) ;
233
243
} else {
234
244
return new p5 . Vector ( this . x , this . y , this . z ) ;
235
245
}
@@ -1125,8 +1135,8 @@ p5.Vector.prototype.cross = function cross(v) {
1125
1135
const x = this . y * v . z - this . z * v . y ;
1126
1136
const y = this . z * v . x - this . x * v . z ;
1127
1137
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 ) ;
1130
1140
} else {
1131
1141
return new p5 . Vector ( x , y , z ) ;
1132
1142
}
@@ -1456,7 +1466,7 @@ p5.Vector.prototype.setMag = function setMag(n) {
1456
1466
*/
1457
1467
p5 . Vector . prototype . heading = function heading ( ) {
1458
1468
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 ) ;
1460
1470
return h ;
1461
1471
} ;
1462
1472
@@ -1547,7 +1557,7 @@ p5.Vector.prototype.setHeading = function setHeading(a) {
1547
1557
*/
1548
1558
p5 . Vector . prototype . rotate = function rotate ( a ) {
1549
1559
let newHeading = this . heading ( ) + a ;
1550
- if ( this . p5 ) newHeading = this . p5 . _toRadians ( newHeading ) ;
1560
+ if ( this . isPInst ) newHeading = this . _toRadians ( newHeading ) ;
1551
1561
const mag = this . mag ( ) ;
1552
1562
this . x = Math . cos ( newHeading ) * mag ;
1553
1563
this . y = Math . sin ( newHeading ) * mag ;
@@ -1629,8 +1639,8 @@ p5.Vector.prototype.angleBetween = function angleBetween(v) {
1629
1639
let angle ;
1630
1640
angle = Math . acos ( Math . min ( 1 , Math . max ( - 1 , dotmagmag ) ) ) ;
1631
1641
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 ) ;
1634
1644
}
1635
1645
return angle ;
1636
1646
} ;
0 commit comments