-
Notifications
You must be signed in to change notification settings - Fork 0
/
Algebra.c
70 lines (60 loc) · 1.3 KB
/
Algebra.c
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
#include <math.h>
#include <assert.h>
#include "Algebra.h"
#define PI 3.14159265358979323846264338327950288
GLfloat *Add3(GLfloat result[3], const GLfloat v1[3], const GLfloat v2[3])
{
for (unsigned i = 0; i < 3; ++i) {
result[i] = v1[i] + v2[i];
}
return result;
}
GLfloat *Scalar3(GLfloat result[3], GLfloat s, GLfloat v[3])
{
for (unsigned i = 0; i < 3; ++i) {
result[i] = s*v[i];
}
return result;
}
GLfloat *Saxpy3(
GLfloat result[3],
const GLfloat v1[3],
GLfloat s,
const GLfloat v2[3]
)
{
for (unsigned i = 0; i < 3; ++i) {
result[i] = v1[i] + s*v2[i];
}
return result;
}
GLfloat *Cross3(
GLfloat result[restrict 3],
const GLfloat v1[3],
const GLfloat v2[3])
{
result[0] = v1[1]*v2[2] - v1[2]*v2[1];
result[1] = v1[2]*v2[0] - v1[0]*v2[2];
result[2] = v1[0]*v2[1] - v1[1]*v2[0];
return result;
}
GLfloat Dot3(const GLfloat v1[3], const GLfloat v2[3])
{
return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
}
GLfloat Length3(const GLfloat v[3])
{
return sqrtf(Dot3(v, v));
}
GLfloat *Normalize3(GLfloat result[3], const GLfloat v[3])
{
GLfloat length = Length3(v);
for (unsigned i = 0; i < 3; ++i) {
result[i] = v[i]/length;
}
return result;
}
GLfloat Angle3(const GLfloat v1[3], const GLfloat v2[3])
{
return 180*acosf(Dot3(v1, v2) / (Length3(v1) * Length3(v2))) / PI;
}