📄 intvec2.h
字号:
#ifndef _INTVEC2_H
#define _INTVEC2_H
class IntVec2
{
public:
enum { SIZE=2 };
int x;
int y;
IntVec2() {}
IntVec2( int u, int v ) : x(u), y(v) {}
/** Component-wise addition of vectors. */
IntVec2& operator+=( const IntVec2& other ) {for ( int i = 0 ; i < SIZE ; ++i ) (&x)[i] += (&other.x)[i]; return *this;}
/** Component-wise subtraction of vectors. */
IntVec2& operator-=( const IntVec2& other ) {for ( int i = 0 ; i < SIZE ; ++i ) (&x)[i] -= (&other.x)[i]; return *this;}
/** Component-wise scalar multiplication. */
IntVec2& operator*=( int s ) {for ( int i = 0 ; i < SIZE ; ++i ) (&x)[i] *= s; return *this;}
/** Access to ith component of the vector. */
int& operator[]( int elementIndex ) {return *((&x)+elementIndex);}
/** Returns a random-access iterator to the first component. */
int* begin() {return &x;}
/** Returns a random-access iterator that points one beyond the last component. */
int* end() {return &x + SIZE;}
/** Returns component-wise addition of vectors. */
IntVec2 operator+( const IntVec2& other ) const {IntVec2 v; for ( int i = 0 ; i < SIZE ; ++i ) (&v.x)[i] = (&x)[i] + (&other.x)[i]; return v;}
/** Returns component-wise subtraction of vectors. */
IntVec2 operator-( const IntVec2& other ) const {IntVec2 v; for ( int i = 0 ; i < SIZE ; ++i ) (&v.x)[i] = (&x)[i] - (&other.x)[i]; return v;}
/** Returns component-wise negation. */
IntVec2 operator-() const {IntVec2 v; for ( int i = 0 ; i < SIZE ; ++i ) (&v.x)[i] = -(&x)[i]; return v;}
/** Returns vector multiplied by given scalar */
IntVec2 operator*( int s ) const {IntVec2 v; for ( int i = 0 ; i < SIZE ; ++i ) (&v.x)[i] = (&x)[i] * s; return v;}
/** Returns ith component of the vector. */
const int& operator[]( int elementIndex ) const {return *((&x)+elementIndex);}
/** Component-wise equality. */
bool operator==( const IntVec2& other ) const {for ( int i = 0 ; i < SIZE ; ++i ) if ( (&x)[i] != (&other.x)[i] ) return false; return true;}
/** Component-wise inequality. */
bool operator!=( const IntVec2& other ) const {for ( int i = 0 ; i < SIZE ; ++i ) if ( (&x)[i] != (&other.x)[i] ) return true; return false;}
/** Returns dot/scalar/inner product of *this and Other */
int dot( const IntVec2& other ) const {int d(0); for ( int i = 0 ; i < SIZE ; ++i ) d += (&x)[i] * (&other.x)[i]; return d;}
/** Returns a random-access iterator to the first component. */
const int* begin() const {return &x;}
/** Returns a random-access iterator that points one beyond the last component. */
const int* end() const {return &x + SIZE;}
};
#endif // _INTVEC2_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -