61 lines
990 B

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. void Vector3::operator*=(float scale)
  12. {
  13. x *= scale;
  14. y *= scale;
  15. z *= scale;
  16. }
  17. Vector3 Vector3::operator/(float scale) const
  18. {
  19. return Vector3(x/scale, y/scale, z/scale);
  20. }
  21. void Vector3::operator/=(float scale)
  22. {
  23. x /= scale;
  24. y /= scale;
  25. z /= scale;
  26. }
  27. void Vector3::operator=(const Vector3 & v)
  28. {
  29. x = v.x;
  30. y = v.y;
  31. z = v.z;
  32. }
  33. void Vector3::Normalize()
  34. {
  35. float magnitude = Magnitude();
  36. x /= magnitude;
  37. y /= magnitude;
  38. z /= magnitude;
  39. }
  40. float Vector3::Magnitude()
  41. {
  42. return sqrtf(x*x + y*y + z*z);
  43. }
  44. Vector3 operator*(float f, const Vector3 & v)
  45. {
  46. return Vector3(f*v.x, f*v.y, f*v.z);
  47. }
  48. float Dot(const Vector3 & l, const Vector3 & r)
  49. {
  50. return l.x * r.x + l.y * r.y + l.z * r.z;
  51. }