-
Notifications
You must be signed in to change notification settings - Fork 1
/
NA_Vector.cpp
88 lines (74 loc) · 1.73 KB
/
NA_Vector.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
//adapted from 1st year maths assignment (lecturer: Gordan Dickers)
#include "NA_Vector.h"
#include <math.h> //for sqrt in normalise - should redo to use custom math lib which has a lookup table for this
//construct
NA_Vector::NA_Vector(float x, float y, float z, float w)
{
this->x = x;
this->y = y;
this->z = z;
this->w = w;
}
NA_Vector::NA_Vector(const NA_Vector & copy)
{
this->x = copy.x;
this->y = copy.y;
this->z = copy.z;
this->w = copy.w;
}
void NA_Vector::scale(float s)
{
x= s*x;
y= s*y;
z= s*z;
w= s*w;
}
void NA_Vector::add(NA_Vector & v1)
{
x= x + v1.x;
y= y + v1.y;
z = z + v1.z;
w = w + v1.w;
//correctW();
}
void NA_Vector::normalise(void)
{
float l = length();
x = x/l;
y = y/l;
z = z/l;
//correctW();
}
float NA_Vector::dist(NA_Vector & v1)
{
float xCom, yCom, zCom = 0;
xCom = x - v1.x;
yCom = y - v1.y;
zCom = z - v1.z;
return sqrt(xCom*xCom + yCom*yCom + zCom*zCom);
}
float NA_Vector::dot(NA_Vector & v1)
{
return x*v1.x + y*v1.y + z*v1.z + w*v1.w;
}
float NA_Vector::length()
{
return sqrt(x*x + y*y + z*z);
}
NA_Vector NA_Vector::twoPointsIntoVector(NA_Vector &startPoint, NA_Vector &endPoint)
{
NA_Vector v;
v.x = (endPoint.x - startPoint.x);
v.y = (endPoint.y - startPoint.y);
v.z = (endPoint.z - startPoint.z);
v.w = (endPoint.w - startPoint.w);
return v;
}
float NA_Vector::clockwiseAngle(NA_Vector & v1)
{
//http://stackoverflow.com/questions/14066933/direct-way-of-computing-clockwise-clockwiseAngle-between-2-vectors/16544330#16544330
float lenSq1, lenSq2;
lenSq1 = x*x + y*y + z*z +w*w;
lenSq2 = v1.x*v1.x + v1.y*v1.y + v1.z*v1.z + v1.w*v1.w;
return acos(dot(v1) / sqrt(lenSq1 * lenSq2));
}