MCPcopy
hub / github.com/processing/p5.js / angleBetween

Method angleBetween

src/math/p5.Vector.js:2299–2314  ·  view source on GitHub ↗

* Calculates the angle between two vectors. * * The angles returned are signed, which means that * `v1.angleBetween(v2) === -v2.angleBetween(v1)`. * * If the vector was created with * createVector() , `angleBetween()` returns * angles in the units

(v)

Source from the content-addressed store, hash-verified

2297 * }
2298 */
2299 angleBetween(v) {
2300 const magSqMult = this.magSq() * v.magSq();
2301 // Returns NaN if either vector is the zero vector.
2302 if (magSqMult === 0) {
2303 return NaN;
2304 }
2305 const u = this.cross(v);
2306 // The dot product computes the cos value, and the cross product computes
2307 // the sin value. Find the angle based on them. In addition, in the case of
2308 // 2D vectors, a sign is added according to the direction of the vector.
2309 let angle = Math.atan2(u.mag(), this.dot(v)) * Math.sign(u.z || 1);
2310 if (this.isPInst) {
2311 angle = this._fromRadians(angle);
2312 }
2313 return angle;
2314 }
2315
2316 /**
2317 * Calculates new `x`, `y`, and `z` components that are proportionally the

Callers 2

p5.Camera.jsFile · 0.80
p5.Vector.jsFile · 0.80

Calls 4

magSqMethod · 0.95
crossMethod · 0.95
dotMethod · 0.95
magMethod · 0.80

Tested by

no test coverage detected