Added arithmetic operators
This commit is contained in:
parent
471a6ce36d
commit
59942d3de0
|
@ -17,6 +17,8 @@ namespace sdlu
|
||||||
T x; ///< x component
|
T x; ///< x component
|
||||||
T y; ///< y component
|
T y; ///< y component
|
||||||
|
|
||||||
|
//################## CONSTRUCTORS ##################//
|
||||||
|
|
||||||
/// Initializes a zero vector
|
/// Initializes a zero vector
|
||||||
Vector2() :
|
Vector2() :
|
||||||
x(0), y(0)
|
x(0), y(0)
|
||||||
|
@ -37,8 +39,107 @@ namespace sdlu
|
||||||
{
|
{
|
||||||
// Empty
|
// 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<unsigned int> Vector2u, Vec2u;
|
||||||
typedef Vector2<int> Vector2i, Vec2i;
|
typedef Vector2<int> Vector2i, Vec2i;
|
||||||
typedef Vector2<float> Vector2f, Vec2f;
|
typedef Vector2<float> Vector2f, Vec2f;
|
||||||
|
|
|
@ -4,7 +4,10 @@
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
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;
|
std::cout << "Vector2f: " << vec.x << ", " << vec.y << std::endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in a new issue