afVec2.h
资源名称:AirForce.rar [点击查看]
上传用户:kaiguan
上传日期:2007-10-28
资源大小:1074k
文件大小:8k
源码类别:
其他游戏
开发平台:
Visual C++
- #ifndef AF_VEC2
- #define AF_VEC2
- /// 2D vector class
- class afVec2
- {
- public:
- enum Axis { X_AXIS, Y_AXIS };
- float x, y;
- /// Creates a default vec2 (0,0)
- afVec2();
- /// Creates a vec2 from to floats
- afVec2(float x,float y);
- /// Creates a vec2 from a pointer to two floats
- afVec2(float *nValues) { set(nValues); }
- /// Creates a vec2 as a copy from an other one
- afVec2(const afVec2& other);
- /// access to members (x,y) by indices
- float& operator[]( unsigned index ) { return (&x)[index]; }
- /// access to members (x,y) by indices
- const float& operator[]( unsigned index ) const { return (&x)[index]; }
- /// copy a vec2 from an other
- inline afVec2& operator=(const afVec2&);
- /// addition same as for normal value type
- inline afVec2 operator+() const;
- /// add an other vec2 to this one
- inline afVec2& operator+=(const afVec2&);
- /// substraction same as for normal value type
- inline afVec2 operator-() const;
- /// substract an other vec2 to this one
- inline afVec2& operator-=(const afVec2&);
- /// multiply this vec2 with a float
- inline afVec2& operator*=(float mult);
- /// divide this vec2 by a float
- inline afVec2& operator/=(float div);
- friend inline afVec2
- operator+(const afVec2& left, const afVec2& right);
- friend inline afVec2
- operator-(const afVec2& left, const afVec2& right);
- friend inline afVec2
- operator/(const afVec2& left, const afVec2& right);
- friend inline bool
- operator==(const afVec2& left, const afVec2& right);
- friend inline float
- operator*(const afVec2& left, const afVec2& right);
- /// Adds two vec2 and saves the result in this vec2
- inline afVec2&
- add(const afVec2& v1, const afVec2& v2);
- /// Adds two vec2 scaling the second one and saves the result in this vec2
- inline afVec2&
- addScaled(const afVec2& v1, float s, const afVec2& v2);
- /// Returns true if this vec2 and nV are at maximum nTol different
- inline bool
- almostEqual(const afVec2& nV, float nTol) const;
- /// Adds two vec2 scaling both and saves the result in this vec2
- inline afVec2&
- combine(float s1, const afVec2& v1, float s2, const afVec2& v2);
- /// Same a operator=()
- inline afVec2&
- copy(const afVec2& v);
- /// Returns the arithmetic distance between this vec2 and nV
- float
- distance(const afVec2& nV) const;
- /// Returns the squared arithmetic distance between this vec2 and nV
- inline float
- sqrDistance(const afVec2& nV) const;
- /// Returns the dot product between this vec2 and nV
- inline float
- dot(const afVec2& nV) const;
- /// Same as operator==()
- inline bool
- equal(const afVec2& v) const;
- /// Returns the length of this vec2
- float
- length() const;
- /// Negates this vec2
- inline void
- negate();
- /// Normalizes this vec2
- float
- normalize();
- /// Multiplies this vec2 by nS
- inline afVec2&
- scale(float nS);
- /// Multiplies nV by nS and stores the result in this vec2
- inline afVec2&
- scale(float nS, const afVec2& nV);
- /// Multiplies this vector component-wise by tensor
- inline afVec2&
- scaleBy( const afVec2& tensor );
- /// Sets new x and y values
- inline void
- set(float x, float y);
- /// Sets new x and y values
- inline void
- set(const float xy[2]);
- /// Subtracts two vec2 and stores the result in this vec2
- inline afVec2&
- sub(const afVec2& v1, const afVec2& v2);
- /// Returns the angle between the vector and one axis
- static float
- cosine(const afVec2& left, Axis nAxis = X_AXIS);
- /// Return the angle between the two vectors
- static float
- cosine(const afVec2& left,const afVec2& right);
- };
- typedef afVec2 *afVec2Ptr;
- inline
- afVec2::afVec2()
- {
- x = 0.0f;
- y = 0.0f;
- }
- inline
- afVec2::afVec2(float ix, float iy)
- {
- x = ix;
- y = iy;
- }
- inline
- afVec2::afVec2(const afVec2& other)
- {
- x = other.x;
- y = other.y;
- }
- inline
- afVec2& afVec2::operator=(const afVec2& vec)
- {
- x = vec.x;
- y = vec.y;
- return *this;
- }
- inline
- afVec2 afVec2::operator+() const
- {
- return *this;
- }
- inline
- afVec2& afVec2::operator+=(const afVec2& vec)
- {
- x += vec.x;
- y += vec.y;
- return *this;
- }
- inline
- afVec2 afVec2::operator-() const
- {
- return afVec2(-x, -y);
- }
- inline
- afVec2& afVec2::operator-=(const afVec2& sub)
- {
- x -= sub.x;
- y -= sub.y;
- return *this;
- }
- inline
- afVec2 operator*(const afVec2& vec, float s)
- {
- return afVec2(vec.x*s, vec.y*s);
- }
- inline
- afVec2 operator*(float s, const afVec2& vec)
- {
- return afVec2(s*vec.x, s*vec.y);
- }
- inline
- afVec2 operator/(const afVec2& vec, float div)
- {
- return afVec2(vec.x/div, vec.y/div);
- }
- inline
- afVec2& afVec2::operator*=(float mult)
- {
- x *= mult;
- y *= mult;
- return *this;
- }
- inline
- afVec2& afVec2::operator/=(float div)
- {
- x /= div;
- y /= div;
- return *this;
- }
- inline afVec2
- operator+(const afVec2& left, const afVec2& right)
- {
- return afVec2(left.x+right.x,left.y+right.y);
- }
- inline afVec2
- operator-(const afVec2& left, const afVec2& right)
- {
- return afVec2(left.x-right.x,left.y-right.y);
- }
- inline afVec2
- operator/(const afVec2& left, const afVec2& right)
- {
- return afVec2(left.x/right.x,left.y/right.y);
- }
- inline bool
- operator==(const afVec2& left, const afVec2& right)
- {
- return ( left.x == right.x && left.y == right.y);
- }
- inline bool
- operator!=(const afVec2& left, const afVec2& right)
- {
- return ( left.x!=right.x
- || left.y!=right.y );
- }
- inline float
- operator*(const afVec2& left, const afVec2& right)
- {
- return (left[0]*right[0]+left[1]*right[1]);
- }
- inline
- afVec2& afVec2::add(const afVec2& vec1, const afVec2& vec2)
- {
- x = vec1.x+vec2.x;
- y = vec1.y+vec2.y;
- return *this;
- }
- inline
- afVec2& afVec2::addScaled(const afVec2& vec1, float s, const afVec2& vec2)
- {
- x = vec1.x+s*vec2.x;
- y = vec1.y+s*vec2.y;
- return *this;
- }
- inline
- bool afVec2::almostEqual(const afVec2& vec, float tol) const
- {
- float dx = x-vec.x;
- float dy = y-vec.y;
- return (dx>-tol && dx<tol && dy>-tol && dy<tol);
- }
- inline
- bool almostEqual(const afVec2 vec1, const afVec2 vec2, float epsilon)
- {
- return vec1.almostEqual(vec2, epsilon);
- }
- inline
- afVec2& afVec2::combine(float s1, const afVec2& vec1, float s2, const afVec2& vec2)
- {
- x = s1*vec1.x+s2*vec2.x;
- y = s1*vec1.y+s2*vec2.y;
- return *this;
- }
- inline
- afVec2& afVec2::copy(const afVec2& vec)
- {
- x = vec.x;
- y = vec.y;
- return *this;
- }
- inline
- float afVec2::sqrDistance(const afVec2& vec) const
- {
- return (x-vec.x)*(x-vec.x)
- +(y-vec.y)*(y-vec.y);
- }
- inline
- float afVec2::dot(const afVec2& vec) const
- {
- return (x*vec.x+y*vec.y);
- }
- inline
- float Dot(const afVec2& vec1, const afVec2& vec2)
- {
- return (vec1.x*vec2.x+vec1.y*vec2.y);
- }
- inline
- bool afVec2::equal(const afVec2& vec) const
- {
- return ( x==vec.x && y==vec.y );
- }
- inline
- void afVec2::negate()
- {
- x = -x;
- y = -y;
- }
- inline
- afVec2& afVec2::scale(float s)
- {
- x *= s;
- y *= s;
- return *this;
- }
- inline
- afVec2& afVec2::scale(float s, const afVec2& vec)
- {
- x = s*vec.x;
- y = s*vec.y;
- return *this;
- }
- inline
- afVec2& afVec2::scaleBy( const afVec2& tensor )
- {
- x *= tensor.x;
- y *= tensor.y;
- return *this;
- }
- inline
- void afVec2::set(float ix, float iy)
- {
- x = ix;
- y = iy;
- }
- inline
- void afVec2::set(const float xy[])
- {
- x = xy[0];
- y = xy[1];
- }
- inline
- afVec2& afVec2::sub(const afVec2& vec1, const afVec2& vec2)
- {
- x = vec1.x-vec2.x;
- y = vec1.y-vec2.y;
- return *this;
- }
- #endif