diff --git a/etk/math/Vector2D.cpp b/etk/math/Vector2D.cpp index 4c1d7ba..866a1e7 100644 --- a/etk/math/Vector2D.cpp +++ b/etk/math/Vector2D.cpp @@ -85,6 +85,24 @@ std::ostream& etk::operator <<(std::ostream& _os, const std::vector& _ob return _os; } +vec2 vec2rotate(const vec2& _val, const vec2& _point, float _angle) { + vec2 out = _val; + float sinAngle = std::sin(_angle); + float cosAngle = std::cos(_angle); + if (_point == vec2(0,0)) { + float tempX = out.x() * cosAngle - out.y() * sinAngle; + float xVal = out.x(); + out.setValue(tempX, + out.y() * cosAngle + xVal * sinAngle); + } else { + float tempX = out.x() - _point.x(); + float tempY = out.y() - _point.y(); + out.setValue(tempX * cosAngle - tempY * sinAngle + _point.x(), + tempY * cosAngle + tempX * sinAngle + _point.y()); + } + return out; +} + namespace etk { template<> Vector2D::operator std::string() const { std::string str; diff --git a/etk/math/Vector2D.h b/etk/math/Vector2D.h index a52be30..b2fde3f 100644 --- a/etk/math/Vector2D.h +++ b/etk/math/Vector2D.h @@ -176,6 +176,19 @@ namespace etk { tmpp.m_floats[1] /= _val; return tmpp; } + /* **************************************************** + * / operator + *****************************************************/ + const Vector2D& operator/= (const Vector2D& _obj) { + m_floats[0] /= _obj.m_floats[0]; + m_floats[1] /= _obj.m_floats[1]; + return *this; + } + const Vector2D& operator/= (const T _val) { + m_floats[0] /= _val; + m_floats[1] /= _val; + return *this; + } /* **************************************************** * ++ operator *****************************************************/ @@ -240,8 +253,9 @@ namespace etk { * @brief Normalize this vector * x^2 + y^2 + z^2 = 1 */ - Vector3D& normalize() { - return *this /= length(); + Vector2D& normalize() { + *this /= length(); + return *this; } /** * @brief Return a normalized version of this vector @@ -374,6 +388,8 @@ inline vec2 vec2ClipInt64(const vec2& _val) { return vec2((int64_t)_val.x(), (int64_t)_val.y()); } +vec2 vec2rotate(const vec2& _val, const vec2& _point, float _angle); + namespace etk { std::ostream& operator <<(std::ostream& _os, const std::vector& _obj); //! @previous