Vector2.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2022-08-05
  6. * Changed: 2022-11-08
  7. *
  8. * */
  9. #include <ls_math/vector/Vector2.hpp>
  10. #include <limits>
  11. #include <cmath>
  12. ls::math::vector::Vector2::Vector2(const ls::math::core::type::vector2_component &_x, const ls::math::core::type::vector2_component &_y)
  13. {
  14. this->components[0] = _x;
  15. this->components[1] = _y;
  16. }
  17. ls::math::vector::Vector2 ls::math::vector::Vector2::operator+(const ls::math::vector::Vector2& _vector) const
  18. {
  19. return ls::math::vector::Vector2::_add(*this, _vector);
  20. }
  21. ls::math::vector::Vector2 ls::math::vector::Vector2::operator-(const ls::math::vector::Vector2& _vector) const
  22. {
  23. return ls::math::vector::Vector2::_subtract(*this, _vector);
  24. }
  25. ls::math::core::type::vector_scalar ls::math::vector::Vector2::operator*(const ls::math::vector::Vector2& _vector) const
  26. {
  27. return ls::math::vector::Vector2::_dot(*this, _vector);
  28. }
  29. ls::math::vector::Vector2 ls::math::vector::Vector2::operator*(double _value)
  30. {
  31. return ls::math::vector::Vector2{components[0] * _value, components[1] * _value};
  32. }
  33. ls::math::vector::Vector2 ls::math::vector::Vector2::operator/(double _divisor)
  34. {
  35. return ls::math::vector::Vector2::_operatorDivision(this->components[0], this->components[1], _divisor);
  36. }
  37. bool ls::math::vector::Vector2::operator==(const ls::math::vector::Vector2 &_vector)
  38. {
  39. return this->components[0] == _vector.components[0] && this->components[1] == _vector.components[1];
  40. }
  41. bool ls::math::vector::Vector2::operator!=(const ls::math::vector::Vector2 &_vector)
  42. {
  43. return this->components[0] != _vector.components[0] || this->components[1] != _vector.components[1];
  44. }
  45. ls::math::vector::Vector2 ls::math::vector::Vector2::add(const ls::math::vector::Vector2 &_leftAddend, const ls::math::vector::Vector2 &_rightAddend)
  46. {
  47. return ls::math::vector::Vector2::_add(_leftAddend, _rightAddend);
  48. }
  49. ls::math::core::type::vector_scalar ls::math::vector::Vector2::dot(const ls::math::vector::Vector2 &_leftAddend, const ls::math::vector::Vector2 &_rightAddend)
  50. {
  51. return ls::math::vector::Vector2::_dot(_leftAddend, _rightAddend);
  52. }
  53. ls::math::core::type::vector2_components ls::math::vector::Vector2::getComponents()
  54. {
  55. return this->components;
  56. }
  57. double ls::math::vector::Vector2::getLength()
  58. {
  59. return ls::math::vector::Vector2::_getLength(this->components[0], this->components[1]);
  60. }
  61. ls::math::core::type::vector2_component ls::math::vector::Vector2::getX()
  62. {
  63. return this->components[0];
  64. }
  65. ls::math::core::type::vector2_component ls::math::vector::Vector2::getY()
  66. {
  67. return this->components[1];
  68. }
  69. ls::math::vector::Vector2 ls::math::vector::Vector2::normalize()
  70. {
  71. double length = ls::math::vector::Vector2::_getLength(this->components[0], this->components[1]) + std::numeric_limits<double>::min();
  72. return ls::math::vector::Vector2::_operatorDivision(this->components[0], this->components[1], length);
  73. }
  74. void ls::math::vector::Vector2::setX(const ls::math::core::type::vector2_component &_value)
  75. {
  76. this->components[0] = _value;
  77. }
  78. void ls::math::vector::Vector2::setY(const ls::math::core::type::vector2_component &_value)
  79. {
  80. this->components[1] = _value;
  81. }
  82. ls::math::vector::Vector2 ls::math::vector::Vector2::subtract(const ls::math::vector::Vector2 &_leftAddend, const ls::math::vector::Vector2 &_rightAddend)
  83. {
  84. return ls::math::vector::Vector2::_subtract(_leftAddend, _rightAddend);
  85. }
  86. ls::math::vector::Vector2 ls::math::vector::Vector2::_add(const ls::math::vector::Vector2 &_leftAddend, const ls::math::vector::Vector2 &_rightAddend)
  87. {
  88. return ls::math::vector::Vector2{_leftAddend.components[0] + _rightAddend.components[0], _leftAddend.components[1] + _rightAddend.components[1]};
  89. }
  90. ls::math::core::type::vector_scalar ls::math::vector::Vector2::_dot(const ls::math::vector::Vector2 &_leftAddend, const ls::math::vector::Vector2 &_rightAddend)
  91. {
  92. return _leftAddend.components[0] * _rightAddend.components[0] + _leftAddend.components[1] * _rightAddend.components[1];
  93. }
  94. ls::math::vector::Vector2 ls::math::vector::Vector2::_subtract(const ls::math::vector::Vector2 &_leftAddend, const ls::math::vector::Vector2 &_rightAddend)
  95. {
  96. return ls::math::vector::Vector2{_leftAddend.components[0] - _rightAddend.components[0], _leftAddend.components[1] - _rightAddend.components[1]};
  97. }
  98. double ls::math::vector::Vector2::_getLength(const ls::math::core::type::vector2_component& _x, const ls::math::core::type::vector2_component& _y)
  99. {
  100. return sqrt(_x * _x + _y * _y);
  101. }
  102. ls::math::vector::Vector2 ls::math::vector::Vector2::_operatorDivision(const ls::math::core::type::vector2_component &_x, const ls::math::core::type::vector2_component &_y, double _divisor)
  103. {
  104. return ls::math::vector::Vector2{_x / _divisor, _y / _divisor};
  105. }