175 lines
3.1 KiB
C++
175 lines
3.1 KiB
C++
#ifndef __Vector2D__
|
|||
|
|
#define __Vector2D__
|
||
|
|
#include "Math.h"
|
||
|
|
|
||
|
|
namespace LifeMath
|
||
|
|
{
|
||
|
|
#undef INLINE
|
||
|
|
#ifdef _WIN32
|
||
|
|
#ifdef _MSC_VER
|
||
|
|
#define INLINE __forceinline
|
||
|
|
#endif
|
||
|
|
#else
|
||
|
|
#ifdef __GNUC__
|
||
|
|
#define INLINE __attribute__((always_inline))
|
||
|
|
#endif
|
||
|
|
#endif
|
||
|
|
|
||
|
|
#ifndef INLINE
|
||
|
|
#define INLINE inline
|
||
|
|
#endif
|
||
|
|
|
||
|
|
template<typename T>
|
||
|
|
struct Vector2D
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
T X, Y;
|
||
|
|
INLINE Vector2D(T x, T y)
|
||
|
|
{
|
||
|
|
this->X = x;
|
||
|
|
this->Y = y;
|
||
|
|
}
|
||
|
|
INLINE void Set(T x, T y)
|
||
|
|
{
|
||
|
|
this->X = x;
|
||
|
|
this->Y = y;
|
||
|
|
}
|
||
|
|
|
||
|
|
INLINE Vector2D(void)
|
||
|
|
{
|
||
|
|
this->X = this->Y = 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
INLINE Vector2D(T val)
|
||
|
|
{
|
||
|
|
this->X = val;
|
||
|
|
this->Y = val;
|
||
|
|
}
|
||
|
|
INLINE Vector2D& operator=(const Vector2D& other)
|
||
|
|
{
|
||
|
|
X = other.X;
|
||
|
|
Y = other.Y;
|
||
|
|
return *this;
|
||
|
|
}
|
||
|
|
//unary operators
|
||
|
|
INLINE Vector2D operator -() const
|
||
|
|
{
|
||
|
|
return Vector2D(-X, -Y);
|
||
|
|
}
|
||
|
|
|
||
|
|
INLINE Vector2D operator +() const
|
||
|
|
{
|
||
|
|
return *this;
|
||
|
|
}
|
||
|
|
|
||
|
|
//binary operators
|
||
|
|
INLINE Vector2D<T> operator +(const LifeMath::Vector2D<T> &other) const
|
||
|
|
{
|
||
|
|
return Vector2D<T>(X + other.X, Y + other.Y);
|
||
|
|
}
|
||
|
|
INLINE Vector2D<T> operator -(const LifeMath::Vector2D<T> &other) const
|
||
|
|
{
|
||
|
|
return Vector2D<T>(X - other.X, Y - other.Y);
|
||
|
|
}
|
||
|
|
INLINE Vector2D<T> operator *(T other) const
|
||
|
|
{
|
||
|
|
return Vector2D<T>(X * other, Y * other);
|
||
|
|
}
|
||
|
|
INLINE Vector2D<T> operator /(T other) const
|
||
|
|
{
|
||
|
|
return Vector2D<T>(X / other, Y / other);
|
||
|
|
}
|
||
|
|
|
||
|
|
INLINE operator T * ()
|
||
|
|
{
|
||
|
|
return &X;
|
||
|
|
}
|
||
|
|
INLINE operator const T * () const
|
||
|
|
{
|
||
|
|
return &X;
|
||
|
|
}
|
||
|
|
INLINE bool operator == (const Vector2D& vector) const
|
||
|
|
{
|
||
|
|
return (X == vector.X && Y == vector.Y);
|
||
|
|
}
|
||
|
|
|
||
|
|
INLINE bool operator != (const Vector2D& vector) const
|
||
|
|
{
|
||
|
|
return (X != vector.X || Y != vector.Y);
|
||
|
|
}
|
||
|
|
INLINE Vector2D<T>& operator += (const Vector2D& Vector)
|
||
|
|
{
|
||
|
|
X += Vector.X;
|
||
|
|
Y += Vector.Y;
|
||
|
|
return *this;
|
||
|
|
}
|
||
|
|
INLINE Vector2D<T>& operator -= (const Vector2D& Vector)
|
||
|
|
{
|
||
|
|
X -= Vector.X;
|
||
|
|
Y -= Vector.Y;
|
||
|
|
return *this;
|
||
|
|
}
|
||
|
|
INLINE Vector2D<T>& operator *= (T Value)
|
||
|
|
{
|
||
|
|
X *= Value;
|
||
|
|
Y *= Value;
|
||
|
|
return *this;
|
||
|
|
}
|
||
|
|
INLINE Vector2D<T>& operator /= (T Value)
|
||
|
|
{
|
||
|
|
//assert(Value && "Zero Value");
|
||
|
|
//*this *= (1.0f / Value);
|
||
|
|
this->X /= Value;
|
||
|
|
this->Y /= Value;
|
||
|
|
|
||
|
|
return *this;
|
||
|
|
}
|
||
|
|
INLINE Vector2D<T>& operator += (T Value)
|
||
|
|
{
|
||
|
|
X += Value;
|
||
|
|
Y += Value;
|
||
|
|
return *this;
|
||
|
|
}
|
||
|
|
INLINE Vector2D<T>& operator -= (T Value)
|
||
|
|
{
|
||
|
|
X -= Value;
|
||
|
|
Y -= Value;
|
||
|
|
return *this;
|
||
|
|
}
|
||
|
|
INLINE Vector2D<T>& operator /= (const Vector2D<T>& vector)
|
||
|
|
{
|
||
|
|
X /= vector.X;
|
||
|
|
Y /= vector.Y;
|
||
|
|
return *this;
|
||
|
|
}
|
||
|
|
INLINE Vector2D<T>& operator *= (const Vector2D<T>& vector)
|
||
|
|
{
|
||
|
|
X *= vector.X;
|
||
|
|
Y *= vector.Y;
|
||
|
|
return *this;
|
||
|
|
}
|
||
|
|
|
||
|
|
INLINE float& operator [](int index)
|
||
|
|
{
|
||
|
|
//assert(index < 3 && "Out Of Range");
|
||
|
|
return *(index + (&X));
|
||
|
|
}
|
||
|
|
INLINE float operator [] (int index) const
|
||
|
|
{
|
||
|
|
//assert(index < 2 && "Out Of Range");
|
||
|
|
return * (index + (&X));
|
||
|
|
}
|
||
|
|
INLINE bool operator > (const Vector2D<T>& v) const
|
||
|
|
{
|
||
|
|
return X > v.X && Y > v.Y;
|
||
|
|
}
|
||
|
|
INLINE bool operator < (const Vector2D<T>& v) const
|
||
|
|
{
|
||
|
|
return X < v.X && Y < v.Y;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
typedef Vector2D<float> Vec2f;
|
||
|
|
typedef Vector2D<int> Vec2i;
|
||
|
|
typedef Vector2D<float> float2;
|
||
|
|
}
|
||
|
|
#endif
|