initial commit
[gph-math] / src / vector.cc
1 #include "vector.h"
2 #include "matrix.h"
3
4 namespace gph {
5
6 Vector2::Vector2(const Vector3 &v)
7         : x(v.x), y(v.y)
8 {
9 }
10
11 Vector3::Vector3(const Vector4 &v)
12         : x(v.x), y(v.y), z(v.z)
13 {
14 }
15
16 Vector3 operator *(const Vector3 &v, const Matrix4x4 &m)
17 {
18         float x = v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0] + m[3][0];
19         float y = v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1] + m[3][1];
20         float z = v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2] + m[3][2];
21         return Vector3(x, y, z);
22 }
23
24 Vector3 operator *(const Matrix4x4 &m, const Vector3 &v)
25 {
26         float x = m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3];
27         float y = m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3];
28         float z = m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3];
29         return Vector3(x, y, z);
30 }
31
32 Vector4::Vector4(const Vector3 &v)
33         : x(v.x), y(v.y), z(v.z), w(1.0f)
34 {
35 }
36
37 }       // namespace gph