# Changeset 239 for trunk/src/matrix.hTweet

Ignore:
Timestamp:
Jan 19, 2011, 9:33:36 PM (11 years ago)
Message:

Implement vector addition, subtraction etc. as well as euclidian distance.

File:
1 edited

Unmodified
Added
Removed
• ## trunk/src/matrix.h

 r237 #define __DH_MATRIX_H__ #define OPERATORS(elems) \ inline Vec##elems operator+(Vec##elems const &op) const \ #include #define VECTOR_OP(elems, op) \ inline Vec##elems operator op(Vec##elems const &val) const \ { \ Vec##elems ret; \ for (int n = 0; n < elems; n++) \ ret[n] = (*this)[n] + op[n]; \ ret[n] = (*this)[n] op val[n]; \ return ret; \ } \ \ inline Vec##elems operator-(Vec##elems const &op) const \ } #define SCALAR_OP(elems, op) \ inline Vec##elems operator op(T const &val) const \ { \ Vec##elems ret; \ for (int n = 0; n < elems; n++) \ ret[n] = (*this)[n] - op[n]; \ ret[n] = (*this)[n] op val; \ return ret; \ } #define OPERATORS(elems) \ T& operator[](int n) { return *(&x + n); } \ T const& operator[](int n) const { return *(&x + n); } \ \ VECTOR_OP(elems, -) \ VECTOR_OP(elems, +) \ VECTOR_OP(elems, *) \ VECTOR_OP(elems, /) \ \ SCALAR_OP(elems, -) \ SCALAR_OP(elems, +) \ SCALAR_OP(elems, *) \ SCALAR_OP(elems, /) \ \ inline float len() const \ { \ T acc = 0; \ for (int n = 0; n < elems; n++) \ acc += (*this)[n] * (*this)[n]; \ return sqrtf((float)acc); \ } Vec2() { x = y = 0; } Vec2(T _x, T _y) { x = _x; y = _y; } T& operator[](int n) { return *(&x + n); } T const& operator[](int n) const { return *(&x + n); } OPERATORS(2) Vec3() { x = y = z = 0; } Vec3(T _x, T _y, T _z) { x = _x; y = _y; z = _z; } T& operator[](int n) { return *(&x + n); } OPERATORS(3)
Note: See TracChangeset for help on using the changeset viewer.