game/sdlpp/sdlpp_vector.hpp

57 lines
1.4 KiB
C++

#ifndef SDLPP_HPP_VECTOR
#define SDLPP_HPP_VECTOR
#include "sdlpp_common.hpp"
namespace SDLPP {
template < typename T > class SDLPPSCOPE Vec2D {
public:
Vec2D() = default;
~Vec2D() = default;
Vec2D( T x, T y ) : _x( x ), _y( y ) {}
T getX() const {
return _x;
}
T getY() const {
return _y;
}
Vec2D operator-( const Vec2D &other ) const {
return Vec2D( getX() - other.getX(), getY() - other.getY() );
}
Vec2D operator+( const Vec2D &other ) const {
return Vec2D( getX() + other.getX(), getY() + other.getY() );
}
Vec2D operator*( double multiplier ) const {
return Vec2D( getX() * multiplier, getY() * multiplier );
}
Vec2D operator/( double divisor ) const {
return *this * ( 1.0 / divisor );
}
T operator*( const Vec2D &other ) const {
return getX() * other.getX() + getY() * other.getY();
}
Vec2D &operator+=( const Vec2D &other ) {
*this = *this + other;
return *this;
}
Vec2D &operator-=( const Vec2D &other ) {
*this = *this - other;
return *this;
}
private:
T _x = 0.0;
T _y = 0.0;
};
template < typename T >
Vec2D< T > operator*( double multiplier, const Vec2D< T > &vec ) {
return vec * multiplier;
}
template < typename T >
Vec2D< T > operator/( double divisor, const Vec2D< T > &vec ) {
return vec / divisor;
}
} // namespace SDLPP
#endif