Changeset 237


Ignore:
Timestamp:
Jan 19, 2011, 9:33:13 PM (10 years ago)
Author:
sam
Message:

Implement simple + and - operators for Int2, Float2 etc.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/matrix.h

    r235 r237  
    1717#define __DH_MATRIX_H__
    1818
    19 template <typename T> struct Vector2
     19#define OPERATORS(elems) \
     20    inline Vec##elems<T> operator+(Vec##elems<T> const &op) const \
     21    { \
     22        Vec##elems<T> ret; \
     23        for (int n = 0; n < elems; n++) \
     24            ret[n] = (*this)[n] + op[n]; \
     25        return ret; \
     26    } \
     27 \
     28    inline Vec##elems<T> operator-(Vec##elems<T> const &op) const \
     29    { \
     30        Vec##elems<T> ret; \
     31        for (int n = 0; n < elems; n++) \
     32            ret[n] = (*this)[n] - op[n]; \
     33        return ret; \
     34    }
     35
     36template <typename T> struct Vec2
    2037{
    21     Vector2() { x = y = 0; }
    22     Vector2(T _x, T _y) { x = _x; y = _y; }
     38    Vec2() { x = y = 0; }
     39    Vec2(T _x, T _y) { x = _x; y = _y; }
    2340    T& operator[](int n) { return *(&x + n); }
     41    T const& operator[](int n) const { return *(&x + n); }
     42
     43    OPERATORS(2)
    2444
    2545    union { T x; T a; T i; };
     
    2747};
    2848
    29 typedef Vector2<float> Float2;
    30 typedef Vector2<int> Int2;
     49typedef Vec2<float> Float2;
     50typedef Vec2<int> Int2;
    3151
    32 template <typename T> struct Vector3
     52template <typename T> struct Vec3
    3353{
    34     Vector3() { x = y = z = 0; }
    35     Vector3(T _x, T _y, T _z) { x = _x; y = _y; z = _z; }
     54    Vec3() { x = y = z = 0; }
     55    Vec3(T _x, T _y, T _z) { x = _x; y = _y; z = _z; }
    3656    T& operator[](int n) { return *(&x + n); }
     57
     58    OPERATORS(3)
    3759
    3860    union { T x; T a; T i; };
     
    4163};
    4264
    43 typedef Vector3<float> Float3;
    44 typedef Vector3<int> Int3;
     65typedef Vec3<float> Float3;
     66typedef Vec3<int> Int3;
    4567
    4668#endif // __DH_MATRIX_H__
Note: See TracChangeset for help on using the changeset viewer.