Vector.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #define _USE_MATH_DEFINES
  2. #include <cmath>
  3. #include "Vector.h"
  4. Vec3f::Vec3f(float x, float y, float z)
  5. {
  6. this->x = x;
  7. this->y = y;
  8. this->z = z;
  9. }
  10. /*The length of the vector*/
  11. float Vec3f::Length()
  12. {
  13. return (float)sqrt(x*x+y*y+z*z);
  14. }
  15. void Vec3f::Normalize()
  16. {
  17. float length = this->Length();
  18. if (length != 0)
  19. {
  20. x = x / length;
  21. y = y / length;
  22. z = z / length;
  23. }
  24. }
  25. float Vec3f::Distance(const Vec3f &other)
  26. {
  27. return (float)sqrt(pow(other.x - x, 2)+pow(other.y - y, 2)+pow(other.z - z, 2));
  28. }
  29. Vec3f::Vec3f()
  30. {
  31. this->x = 0;
  32. this->y = 0;
  33. this->z = 0;
  34. }
  35. Vec3f::Vec3f(const Vec3f &other)
  36. {
  37. this->x = other.x;
  38. this->y = other.y;
  39. this->z = other.z;
  40. }
  41. float& Vec3f::operator [](int index)
  42. {
  43. return v[index];
  44. }
  45. Vec3f Vec3f::operator+(const Vec3f & other)
  46. {
  47. return Vec3f(x + other.x, y + other.y, z + other.z);
  48. }
  49. Vec3f Vec3f::operator-(const Vec3f & other)
  50. {
  51. return Vec3f(x - other.x, y - other.y, z - other.z);
  52. }
  53. Vec3f Vec3f::operator/(float value)
  54. {
  55. return Vec3f(x / value, y / value, z / value);
  56. }
  57. bool Vec3f::operator==(const Vec3f & other)
  58. {
  59. /*bool xb, yb,zb;
  60. xb = x == other.x;
  61. yb = y == other.y;
  62. zb = z == other.z;
  63. if (xb & yb & zb)
  64. return true;*/
  65. return x == other.x & y == other.y & z == other.z;
  66. }
  67. bool Vec3f::operator!=(const Vec3f & other)
  68. {
  69. return x != other.x & y != other.y & z != other.z;
  70. }
  71. Vec3f Vec3f::operator*(const float & other)
  72. {
  73. return Vec3f(x*other, y*other, z*other);
  74. }
  75. Vec3f Vec3f::cross(const Vec3f & other)
  76. {
  77. return Vec3f(
  78. y*other.z - other.y*z,
  79. z*other.x - other.z*x,
  80. x*other.y - other.x*y
  81. );
  82. }
  83. Vec2f::Vec2f(float x, float y)
  84. {
  85. this->x = x;
  86. this->y = y;
  87. }
  88. Vec2f::Vec2f()
  89. {
  90. this->x = 0;
  91. this->y = 0;
  92. }
  93. Vec2f::Vec2f(const Vec2f &other)
  94. {
  95. this->x = other.x;
  96. this->y = other.y;
  97. }
  98. float& Vec2f::operator [](int index)
  99. {
  100. return v[index];
  101. }
  102. Vec2f Vec2f::operator+(const Vec2f & other)
  103. {
  104. return Vec2f(x + other.x, y+other.y);
  105. }
  106. float Vec2f::length()
  107. {
  108. return (float)sqrt(x*x + y*y);
  109. }