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