-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVector4.h
94 lines (77 loc) · 1.53 KB
/
Vector4.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#ifndef VECTOR4_H
#define VECTOR4_H
#include <cmath>
class Vector4
{
public:
Vector4( float x = 0.0f, float y = 0.0f, float z = 0.0f )
{
xyzw[ 0 ] = x;
xyzw[ 1 ] = y;
xyzw[ 2 ] = z;
xyzw[ 3 ] = 1.0f;
}
~Vector4()
{
}
float length() const
{
return sqrt( xyzw[ 0 ] * xyzw[ 0 ] + xyzw[ 1 ] * xyzw[ 1 ] + xyzw[ 2 ] * xyzw[ 2 ] );
}
void normalize()
{
float l = length();
for( int i = 0; i < 3; i++ )
xyzw[ i ] /= l;
}
const float* getAsArray() const
{
return xyzw;
}
void initFromArray( const float *a )
{
for( int i = 0; i < 3; i++ )
xyzw[ i ] = a[ i ];
xyzw[ 3 ] = 1.0f;
}
void add( const Vector4 &other )
{
const float *f = other.getAsArray();
for( int i = 0; i < 3; i++ )
xyzw[ i ] += f[ i ];
}
void sub( const Vector4 &other )
{
const float *f = other.getAsArray();
for( int i = 0; i < 3; i++ )
xyzw[ i ] -= f[ i ];
}
void mul( float scalar )
{
for( int i = 0; i < 3; i++ )
xyzw[ i ] *= scalar;
}
float dot( const Vector4 &other )
{
float result = 0.0f;
const float *f = other.getAsArray();
for( int i = 0; i < 3; i++ )
result += ( xyzw[ i ] * f[ i ] );
return result;
}
// Vector cross product A x B
// this vector is B
// other is A
Vector4 cross( const Vector4 &other )
{
const float *A = other.getAsArray();
const float *B = getAsArray();
float i = A[ 1 ] * B[ 2 ] - B[ 1 ] * A[ 2 ];
float j = A[ 0 ] * B[ 2 ] - B[ 0 ] * A[ 2 ];
float k = A[ 0 ] * B[ 1 ] - B[ 0 ] * A[ 1 ];
return Vector4( i, j, k );
}
private:
float xyzw[ 4 ];
};
#endif