You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.2 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. #include "Vector3.h"
  2. #include <math.h>
  3. Vector3 Vector3::operator+(const Vector3 & v) const
  4. {
  5. return Vector3(x+v.x, y+v.y, z+v.z);
  6. }
  7. Vector3 Vector3::operator-(const Vector3 & v) const
  8. {
  9. return Vector3(x - v.x, y - v.y, z - v.z);
  10. }
  11. Vector3 Vector3::operator*(const Vector3 & v) const
  12. {
  13. return Vector3(x*v.x, y*v.y, z*v.z);
  14. }
  15. void Vector3::operator*=(float scale)
  16. {
  17. x *= scale;
  18. y *= scale;
  19. z *= scale;
  20. }
  21. Vector3 Vector3::operator/(float scale) const
  22. {
  23. return Vector3(x/scale, y/scale, z/scale);
  24. }
  25. void Vector3::operator/=(float scale)
  26. {
  27. x /= scale;
  28. y /= scale;
  29. z /= scale;
  30. }
  31. void Vector3::operator=(const Vector3 & v)
  32. {
  33. x = v.x;
  34. y = v.y;
  35. z = v.z;
  36. }
  37. void Vector3::Normalize()
  38. {
  39. float magnitude = Magnitude();
  40. x /= magnitude;
  41. y /= magnitude;
  42. z /= magnitude;
  43. }
  44. float Vector3::Magnitude()
  45. {
  46. return sqrtf(x*x + y*y + z*z);
  47. }
  48. Vector3 operator*(float f, const Vector3 & v)
  49. {
  50. return Vector3(f*v.x, f*v.y, f*v.z);
  51. }
  52. float Dot(const Vector3 & l, const Vector3 & r)
  53. {
  54. return l.x * r.x + l.y * r.y + l.z * r.z;
  55. }
  56. Vector3 Cross(const Vector3 & l, const Vector3 & r)
  57. {
  58. return Vector3(l.y*r.z - l.z*r.y, l.z*r.x - l.x*r.z, l.x*r.y - l.y*r.x);
  59. }