Added arithmetic operators
This commit is contained in:
parent
471a6ce36d
commit
59942d3de0
2 changed files with 105 additions and 1 deletions
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue