| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- #ifndef Vec2_H
- #define Vec2_H
- #include "FrameworkMath.h"
- namespace Framework
- {
- //! A two-dimensional vector
- template<typename T> class Vec2
- {
- public:
- T x; //! x component of the vector
- T y; //! y component of the vector
- //! Constructor
- inline Vec2() noexcept
- : x(0),
- y(0)
- {}
- //! Constructor
- //! \param x X component of the vector
- //! \param y Y component of the vector
- inline Vec2(T x, T y)
- : x(x),
- y(y)
- {}
- //! Constructor
- //! \param vect A vector whose components are adopted
- inline Vec2(const Vec2& vect)
- : x((T)vect.x),
- y((T)vect.y)
- {}
- //! Scales the vector so that it has length 1
- inline Vec2& normalize()
- {
- const T length = getLength();
- x /= length;
- y /= length;
- return *this;
- }
- //! Rotates the vector 90 degrees counter-clockwise
- inline Vec2& CCW90()
- {
- T temp = y;
- y = -x;
- x = temp;
- return *this;
- }
- //! Rotates the vector 90 degrees clockwise
- inline Vec2& CW90()
- {
- T temp = y;
- y = x;
- x = -temp;
- return *this;
- }
- //! Swaps the values of the vectors
- //! \param vect The vector to swap with
- inline Vec2& Swap(Vec2& vect)
- {
- const Vec2 tmp = vect;
- vect = *this;
- *this = tmp;
- return *this;
- }
- //! Adopts the values of another vector
- //! \param r The other vector
- inline Vec2 operator=(const Vec2& r)
- {
- x = r.x;
- y = r.y;
- return *this;
- }
- //! Adds another vector to this one
- //! \param r The other vector
- inline Vec2 operator+=(const Vec2& r)
- {
- x += r.x;
- y += r.y;
- return *this;
- }
- //! Subtracts another vector from this one
- //! \param r The other vector
- inline Vec2 operator-=(const Vec2& r)
- {
- x -= r.x;
- y -= r.y;
- return *this;
- }
- //! Scales this vector by a scalar
- //! \param r The scalar
- inline Vec2 operator*=(const T& r)
- {
- x *= r;
- y *= r;
- return *this;
- }
- //! Divides this vector by a scalar
- //! \param r The scalar
- inline Vec2 operator/=(const T& r)
- {
- x /= r;
- y /= r;
- return *this;
- }
- //! Negates the vector
- inline Vec2 operator-() const
- {
- return Vec2<T>(-x, -y);
- }
- //! Converts the vector to a vector of another type
- template<typename T2> inline operator Vec2<T2>() const
- {
- return Vec2<T2>((T2)x, (T2)y);
- }
- //! Calculates the square of the vector's length
- inline T getLengthSq() const
- {
- return *this * *this;
- }
- //! Calculates the length of the vector
- inline T getLength() const
- {
- return (T)sqrt(getLengthSq());
- }
- //! Calculates the dot product between two vectors
- //! \param r The other vector
- inline T operator*(const Vec2& r) const
- {
- return x * r.x + y * r.y;
- }
- //! Adds two vectors
- //! \param r The other vector
- inline Vec2 operator+(const Vec2& r) const
- {
- return Vec2(*this) += r;
- }
- //! Subtracts two vectors
- //! \param r The other vector
- inline Vec2 operator-(const Vec2& r) const
- {
- return Vec2(*this) -= r;
- }
- //! Multiplies the components of two vectors
- //! \param r The other vector
- inline Vec2 operator*(const T& r) const
- {
- return Vec2(*this) *= r;
- }
- //! Divides the components of two vectors
- //! \param r The other vector
- inline Vec2 operator/(const T& r) const
- {
- return Vec2(*this) /= r;
- }
- //! Checks whether the vector is in the rectangle between two vectors
- //! p1: A vector to one corner of the rectangle
- //! p2: A vector to the opposite corner of the rectangle
- inline bool isInRegion(const Vec2& p1, const Vec2& p2) const
- {
- const T medianX = (T)((p1.x + p2.x) / 2.0);
- const T medianY = (T)((p1.y + p2.y) / 2.0);
- return abs<T>(medianX - x) <= abs<T>(medianX - p1.x)
- && abs<T>(medianY - y) <= abs<T>(medianY - p1.y);
- }
- //! Checks two vectors for equality
- //! \param r The other vector
- inline bool operator==(const Vec2& r) const
- {
- return x == r.x && y == r.y;
- }
- //! Checks two vectors for inequality
- //! \param r The other vector
- inline bool operator!=(const Vec2& r) const
- {
- return !(*this == r);
- }
- //! Calculates the midpoint between two vectors
- //! p2: The other vector
- inline Vec2 centerWith(const Vec2& p2) const
- {
- return Vec2((T)((x + p2.x) / 2.0), (T)((y + p2.y) / 2.0));
- }
- //! Rotates the vector counter-clockwise
- //! \param angle The angle in radians
- inline Vec2 rotation(const float angle) const
- {
- Vec2 result;
- float cosine = lowPrecisionCos(angle);
- float sine = lowPrecisionSin(angle);
- result.x = (T)(x * cosine - y * sine);
- result.y = (T)(x * sine + y * cosine);
- return result;
- }
- //! Determines the angle between two vectors
- inline T angle(const Vec2& v2) const
- {
- return (T)atan2((float)(x * v2.y - y * v2.x), (float)(*this * v2));
- }
- };
- } // namespace Framework
- #endif
|