Added arithmetic operators

This commit is contained in:
Robert 2020-05-16 14:00:03 +02:00
parent 471a6ce36d
commit 59942d3de0
2 changed files with 105 additions and 1 deletions

View file

@ -17,6 +17,8 @@ namespace sdlu
T x; ///< x component
T y; ///< y component
//################## CONSTRUCTORS ##################//
/// Initializes a zero vector
Vector2() :
x(0), y(0)
@ -37,8 +39,107 @@ namespace sdlu
{
// Empty
}
//################## OPERATORS ##################//
friend Vector2<T> operator-(const Vector2<T>& right)
{
return Vector2<T>(-right.x, -right.y);
}
friend Vector2<T> operator+(const Vector2<T>& left, const Vector2<T>& right)
{
return Vector2<T>(left.x + right.x, left.y + right.y);
}
friend Vector2<T> operator-(const Vector2<T>& left, const Vector2<T>& right)
{
return left + (-right)
}
friend Vector2<T> operator*(const Vector2<T>& left, const Vector2<T>& right)
{
return Vector2<T>(left.x * right.x, left.y * right.y);
}
friend Vector2<T> operator/(const Vector2<T>& left, const Vector2<T>& right)
{
return Vector2<T>(left.x / right.x, left.y / right.y);
}
friend Vector2<T> operator*(T left, const Vector2<T>& right)
{
return Vector2<T>(left * right.x, left * right.y);
}
friend Vector2<T> operator*(const Vector2<T>& left, T right)
{
return right * left;
}
friend Vector2<T> operator/(const Vector2<T>& left, T right)
{
return Vector2<T>(left.x / right, left.y / right);
}
friend Vector2<T>& operator+=(Vector2<T>& left, const Vector2<T>& right)
{
left.x += right.x;
left.y += right.y;
return left;
}
friend Vector2<T>& operator-=(Vector2<T>& left, const Vector2<T>& right)
{
left += (-right);
return left;
}
friend Vector2<T>& operator*=(Vector2<T>& left, const Vector2<T>& right)
{
left.x *= right.x;
left.y *= right.y;
return left;
}
friend Vector2<T>& operator/(Vector2<T>& left, const Vector2<T>& right)
{
left.x /= right.x;
left.y /= right.y;
return left;
}
friend Vector2<T>& operator*=(Vector2<T>& left, T right)
{
left.x *= right;
left.y *= right;
return left;
}
friend Vector2<T>& operator/=(Vector2<T>& left, T right)
{
left.x /= right;
left.y /= right;
return left;
}
friend bool operator==(const Vector2<T>& left, const Vector2<T>& right)
{
return ((left.x == right.x) && (left.y == right.y));
}
friend bool operator!=(const Vector2<T>& left, const Vector2<T>& right)
{
return !(left == right);
}
};
//################## TYPEDEFS ##################//
typedef Vector2<unsigned int> Vector2u, Vec2u;
typedef Vector2<int> Vector2i, Vec2i;
typedef Vector2<float> Vector2f, Vec2f;

View file

@ -4,7 +4,10 @@
int main(int argc, char** argv)
{
sdlu::Vector2f vec(.4f, -2.3f);
sdlu::Vector2f vecA(.4f, -2.3f);
sdlu::Vector2f vecB(-8.9f, 0.003f);
sdlu::Vector2f vec = vecA + vecB;
std::cout << "Vector2f: " << vec.x << ", " << vec.y << std::endl;
return 0;