Vector2.cpp 4.7 KB

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