-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_operations.cpp
More file actions
69 lines (53 loc) · 1.32 KB
/
Copy pathvector_operations.cpp
File metadata and controls
69 lines (53 loc) · 1.32 KB
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
/**
* vector_operations.cpp
* -------------------------------
* See vector_operations.h for all method documentation
*
* @author Paul Lambert
* @note CS418
* @see vector_operations.h
*/
#include "vector_operations.h"
GLfloat* VectorOperations::mult_v(GLfloat vector[], float mult)
{
GLfloat *multiplied = new GLfloat[3];
for(int i=0; i<3; i++)
{
multiplied[i] = vector[i] * mult;
}
return multiplied;
}
GLfloat* VectorOperations::add_v(GLfloat first[], GLfloat second[])
{
GLfloat *sum = new GLfloat[3];
for(int i=0; i<3; i++)
{
sum[i] = first[i] + second[i];
}
return sum;
}
GLfloat* VectorOperations::cross_product(GLfloat a[], GLfloat b[])
{
GLfloat *cross = new GLfloat[3];
cross[0] = (a[1] * b[2]) - (a[2]* b[1]);
cross[1] = (a[2] * b[0]) - (a[0]* b[2]);
cross[2] = (a[0] * b[1]) - (a[1]* b[0]);
return cross;
}
GLdouble VectorOperations::norm(GLfloat vector[])
{
GLdouble pre_square =
pow(vector[0], 2) +
pow(vector[1], 2) +
pow(vector[2], 2);
return sqrt(pre_square);
}
GLfloat* VectorOperations::unit_v(GLfloat vector[])
{
GLdouble norm = this->norm(vector);
GLfloat *unit = new GLfloat[3];
unit[0] = vector[0] / norm;
unit[1] = vector[1] / norm;
unit[2] = vector[2] / norm;
return unit;
}