# isovector/cs349-a1

Switch branches/tags
Nothing to show
Fetching contributors…
Cannot retrieve contributors at this time
155 lines (119 sloc) 3.42 KB
 // This class is taken from http://willperone.net/Code/vector2.php // it seemed silly to implement my own vector library #ifndef VECTOR_MATH_H #define VECTOR_MATH_H #include #include template struct vector2 { T x, y; //! trivial ctor vector2() {} //! setting ctor vector2(const T x0, const T y0): x(x0), y(y0) {} //! array indexing T &operator [](unsigned int i) { return *(&x+i); } //! array indexing const T &operator [](unsigned int i) const { return *(&x+i); } //! function call operator void operator ()(const T x0, const T y0) { x= x0; y= y0; } //! test for equality bool operator==(const vector2 &v) { return (x==v.x && y==v.y); } //! test for inequality bool operator!=(const vector2 &v) { return (x!=v.x || y!=v.y); } //! set to value const vector2 &operator =(const vector2 &v) { x= v.x; y= v.y; return *this; } //! negation const vector2 operator -(void) const { return vector2(-x, -y); } //! addition const vector2 operator +(const vector2 &v) const { return vector2(x+v.x, y+v.y); } //! subtraction const vector2 operator -(const vector2 &v) const { return vector2(x-v.x, y-v.y); } const vector2 operator *(const vector2 &v) const { return vector2(x*v.x, y*v.y); } const vector2 operator /(const vector2 &v) const { return vector2(x/v.x, y/v.y); } //! uniform scaling const vector2 operator *(const T num) const { vector2 temp(*this); return temp*=num; } //! uniform scaling const vector2 operator /(const T num) const { vector2 temp(*this); return temp/=num; } //! addition const vector2 &operator +=(const vector2 &v) { x+=v.x; y+=v.y; return *this; } //! subtraction const vector2 &operator -=(const vector2 &v) { x-=v.x; y-=v.y; return *this; } //! uniform scaling const vector2 &operator *=(const T num) { x*=num; y*=num; return *this; } //! uniform scaling const vector2 &operator /=(const T num) { x/=num; y/=num; return *this; } //! dot product T dot(const vector2 &v) const { return x*v.x + y*v.y; } }; template struct rectangle { vector2 position; vector2 size; rectangle() {} rectangle(const T &x0, const T &y0, const T &w, const T &h) : position(x0, y0), size(w, h) { } rectangle(const vector2 &pos, const vector2 &siz) : position(pos), size(siz) { } T left() const { return position.x; } T right() const { return position.x + size.x; } T top() const { return position.y; } T bottom() const { return position.y + size.y; } bool intersects(const rectangle &r2) const { return !(r2.left() > right() || r2.right() < left() || r2.top() > bottom() || r2.bottom() < top()); } rectangle intersection(const rectangle &r2) const { T l = std::max(left(), r2.left()); T r = std::min(right(), r2.right()); T t = std::max(top(), r2.top()); T b = std::min(bottom(), r2.bottom()); return rectangle(l, t, r - l, b - t); } }; typedef vector2 vec2; typedef rectangle rect2; #define UNPACK(vec) (vec).x, (vec).y #define UNPACKI(vec) static_cast((vec).x), static_cast((vec).y) #endif