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.

84 lines
1.5 KiB

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