* Rotates a 2D vector to a specific angle without changing its magnitude. * * By convention, the positive x-axis has an angle of 0. Angles increase in * the clockwise direction. * * If the vector was created with * createVector() , `setHeading()` uses
(a)
| 2022 | * } |
| 2023 | */ |
| 2024 | setHeading(a) { |
| 2025 | if (this.dimensions < 2 || ( |
| 2026 | this._values instanceof Array && this._values.slice(2).some(v => v !== 0)) |
| 2027 | ) { |
| 2028 | p5._friendlyError( |
| 2029 | 'p5.Vector.setHeading() only supports 2D vectors (z === 0). ' + |
| 2030 | 'For 3D or higher-dimensional vectors, use rotate() or another ' + |
| 2031 | 'appropriate method instead.', |
| 2032 | 'p5.Vector.setHeading' |
| 2033 | ); |
| 2034 | return this; |
| 2035 | } |
| 2036 | const m = this.mag(); |
| 2037 | this.x = m * Math.cos(a); |
| 2038 | this.y = m * Math.sin(a); |
| 2039 | return this; |
| 2040 | } |
| 2041 | |
| 2042 | /** |
| 2043 | * Rotates a 2D vector by an angle without changing its magnitude. |
no test coverage detected