modeling/src/maths/vec2/normalize.js

  1. /**
  2. * Normalize the given vector.
  3. *
  4. * @param {vec2} out - receiving vector
  5. * @param {vec2} vector - vector to normalize
  6. * @returns {vec2} out
  7. * @alias module:modeling/maths/vec2.normalize
  8. */
  9. const normalize = (out, vector) => {
  10. const x = vector[0]
  11. const y = vector[1]
  12. let len = x * x + y * y
  13. if (len > 0) {
  14. len = 1 / Math.sqrt(len)
  15. }
  16. out[0] = x * len
  17. out[1] = y * len
  18. return out
  19. }
  20. // old this.dividedBy(this.length())
  21. module.exports = normalize