modeling/src/maths/vec3/rotateZ.js

  1. /**
  2. * Rotate the given vector around the given origin, Z axis only.
  3. *
  4. * @param {vec3} out - receiving vector
  5. * @param {vec3} vector - vector to rotate
  6. * @param {vec3} origin - origin of the rotation
  7. * @param {Number} radians - angle of rotation in radians
  8. * @returns {vec3} out
  9. * @alias module:modeling/maths/vec3.rotateZ
  10. */
  11. const rotateZ = (out, vector, origin, radians) => {
  12. const p = []
  13. const r = []
  14. // Translate point to the origin
  15. p[0] = vector[0] - origin[0]
  16. p[1] = vector[1] - origin[1]
  17. // perform rotation
  18. r[0] = (p[0] * Math.cos(radians)) - (p[1] * Math.sin(radians))
  19. r[1] = (p[0] * Math.sin(radians)) + (p[1] * Math.cos(radians))
  20. // translate to correct position
  21. out[0] = r[0] + origin[0]
  22. out[1] = r[1] + origin[1]
  23. out[2] = vector[2]
  24. return out
  25. }
  26. module.exports = rotateZ