@@ -625,6 +625,32 @@ p5.Vector.prototype.rotate = function (a) {
625625 return this ;
626626} ;
627627
628+ /**
629+ * Calculates and returns the angle (in radians) between two vectors.
630+ * @method angleBetween
631+ * @param {p5.Vector } the x, y, and z components of a p5.Vector
632+ * @return {Number } the angle between (in radians)
633+ * @example
634+ * <div class="norender">
635+ * <code>
636+ * var v1 = createVector(1, 0, 0);
637+ * var v2 = createVector(0, 1, 0);
638+ *
639+ * var angle = v1.angleBetween(v2);
640+ * // angle is PI/2
641+ * </code>
642+ * </div>
643+ */
644+ p5 . Vector . prototype . angleBetween = function ( v ) {
645+ var angle = Math . acos ( this . dot ( v ) / ( this . mag ( ) * v . mag ( ) ) ) ;
646+ if ( this . p5 ) {
647+ if ( this . p5 . _angleMode === constants . DEGREES ) {
648+ angle = polarGeometry . radiansToDegrees ( angle ) ;
649+ }
650+ }
651+ return angle ;
652+ } ;
653+
628654/**
629655 * Linear interpolate the vector to another vector
630656 *
@@ -1011,34 +1037,6 @@ p5.Vector.lerp = function (v1, v2, amt, target) {
10111037 return target ;
10121038} ;
10131039
1014- /**
1015- * Calculates and returns the angle (in radians) between two vectors.
1016- * @method angleBetween
1017- * @static
1018- * @param {p5.Vector } v1 the x, y, and z components of a p5.Vector
1019- * @param {p5.Vector } v2 the x, y, and z components of a p5.Vector
1020- * @return {Number } the angle between (in radians)
1021- * @example
1022- * <div class="norender">
1023- * <code>
1024- * var v1 = createVector(1, 0, 0);
1025- * var v2 = createVector(0, 1, 0);
1026- *
1027- * var angle = p5.Vector.angleBetween(v1, v2);
1028- * // angle is PI/2
1029- * </code>
1030- * </div>
1031- */
1032- p5 . Vector . angleBetween = function ( v1 , v2 ) {
1033- var angle = Math . acos ( v1 . dot ( v2 ) / ( v1 . mag ( ) * v2 . mag ( ) ) ) ;
1034- if ( this . p5 ) {
1035- if ( this . p5 . _angleMode === constants . DEGREES ) {
1036- angle = polarGeometry . radiansToDegrees ( angle ) ;
1037- }
1038- }
1039- return angle ;
1040- } ;
1041-
10421040/**
10431041 * @static
10441042 */
0 commit comments