view include/Math.h @ 11:398894c52b69

Changed mind. Sticking with current design. Matrix22<T> is done.
author Eris Caffee <discordia@eldalin.com>
date Fri, 07 Oct 2011 13:33:48 -0500
parents 81d2aa42a860
children 1d2b25d4517f
line source
1 #ifndef MATH_H_
2 #define MATH_H_
4 #include "Vector.h"
5 #include "Matrix.h"
7 namespace arda
8 {
10 ////////////////////////////////////////////////////////////////////////////////
11 // General notes:
12 //
13 // As much as possible is defined inline for performance.
14 // Using typedef'ed arrays for vectors and matrices necessitates a lot of
15 // redundant function names: scale2, scale3, scale4, for example.
16 // This is, however, perfectly in line with the implementation of OpenGL and
17 // allows the library to maintain optimal memory usage and performance. (I hope!)
18 // In any event, I think I could do far worse than to emulate the OpenGL
19 // working group - a group of people who have studied this for years and are
20 // experts in the field.
22 ////////////////////////////////////////////////////////////////////////////////
23 // Angles
25 namespace Math
26 {
27 // This many decimals is enough to cover even IEEE quad precision (128 bit)
28 // reals. Run on PA-RISC lately? :)
29 const double PI = 3.14159265358979323846264338327950288;
31 //////////////////////////////////////////////////////////////////////////
32 // Mixed Vector / Matrix operations.
33 // * *=
34 // Defined for Vector * Matrix and Matrix * Vector, but these
35 // operators are not part of the class. They are defined independently.
36 //
38 // Vector * Matrix
39 template <typename T> inline Vector2<T>& operator*=(Vector2<T>& v, const Matrix22<T>& m)
40 {
41 Vector2<T> vres(0);
42 vres[0] = v[0]*m[0] + v[1]*m[1];
43 vres[1] = v[0]*m[2] + v[1]*m[3];
44 v = vres;
45 return v;
46 }
47 template <typename T> inline Vector2<T> operator*(const Vector2<T>& v, const Matrix22<T>& m)
48 { return Vector2<T>(v) *= m; }
50 // Matrix * Vector
51 // Note: can't define Matrix *= Vector since the result is a Vector.
52 template <typename T> inline Vector2<T> operator*(const Matrix22<T>& m, const Vector2<T>& v)
53 {
54 Vector2<T> vres(0);
55 vres[0] = m[0]*v[0] + m[2]*v[1];
56 vres[1] = m[1]*v[0] + m[3]*v[1];
57 return vres;
58 }
60 } // namespace Math
62 } // namespace arda
64 #endif