-
Notifications
You must be signed in to change notification settings - Fork 0
/
cQuaternion.cpp
88 lines (70 loc) · 1.45 KB
/
cQuaternion.cpp
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
#include "cquaternion.h"
#include <math.h>
cQuaternion::cQuaternion()
{
a = 1;
b = c = d = 0;
}
cQuaternion cQuaternion::operator*(const cQuaternion &other)
{
cQuaternion result;
float a1,b1,c1,d1;
float a2,b2,c2,d2;
a1 = a;
b1 = b;
c1 = c;
d1 = d;
a2 = other.a;
b2 = other.b;
c2 = other.c;
d2 = other.d;
result.a = a1*a2 - b1*b2 - c1*c2 - d1*d2;
result.b = a1*b2 + b1*a2 + c1*d2 - d1*c2;
result.c = a1*c2 - b1*d2 + c1*a2 + d1*b2;
result.d = a1*d2 + b1*c2 - c1*b2 + d1*a2;
return result;
}
cQuaternion cQuaternion::operator*(const float scalar)
{
cQuaternion result;
result.a = a * scalar;
result.b = b * scalar;
result.c = c * scalar;
result.d = d * scalar;
return result;
}
cQuaternion cQuaternion::operator+(const cQuaternion &other)
{
cQuaternion result;
result.a = a + other.a;
result.b = b + other.b;
result.c = c + other.c;
result.d = d + other.d;
return result;
}
cQuaternion cQuaternion::operator-(const cQuaternion &other)
{
cQuaternion result;
result.a = a - other.a;
result.b = b - other.b;
result.c = c - other.c;
result.d = d - other.d;
return result;
}
cQuaternion cQuaternion::conjugate()
{
cQuaternion result;
result.a = a;
result.b = -b;
result.c = -c;
result.d = -d;
return result;
}
void cQuaternion::normalize()
{
float norm = sqrt(a*a + b*b + c*c + d*d);
a = a/norm;
b = b/norm;
c = c/norm;
d = d/norm;
}