game/sdlpp/sdlpp_vector.hpp

72 lines
1.9 KiB
C++

#ifndef SDLPP_HPP_VECTOR
#define SDLPP_HPP_VECTOR
#include "sdlpp_common.hpp"
#include <ostream>
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;
}
bool operator==( const Vec2D &other ) {
return other._x == _x && other._y == _y;
}
bool operator!=( const Vec2D &other ) {
return !( *this == other );
}
template < typename K >
friend std::ostream &operator<<( std::ostream &stream,
const Vec2D< K > &vec );
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;
}
template < typename T >
std::ostream &operator<<( std::ostream &stream, const Vec2D< T > &vec ) {
stream << '(' << vec._x << ", " << vec._y << ")";
return stream;
}
} // namespace SDLPP
#endif