* Limits a vector's magnitude to a maximum value. * * The static version of `limit()`, as in `p5.Vector.limit(v, 5)`, returns a * new p5.Vector object and doesn't change the * original. * * @param {Number} max maximum magnitude for the vector. * @ch
(max)
| 1744 | * } |
| 1745 | */ |
| 1746 | limit(max) { |
| 1747 | const mSq = this.magSq(); |
| 1748 | if (mSq > max * max) { |
| 1749 | this.div(Math.sqrt(mSq)) //normalize it |
| 1750 | .mult(max); |
| 1751 | } |
| 1752 | return this; |
| 1753 | } |
| 1754 | |
| 1755 | /** |
| 1756 | * Sets a vector's magnitude to a given value. |