-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVectorHelpers.cc
160 lines (125 loc) · 2.81 KB
/
VectorHelpers.cc
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include <math.h>
// Vector Helpers
using std::cout;
using std::endl;
int length = 3;
double dotProduct(double x [],double y [])
{
double product = 0;
//int length = sizeof(&x)/sizeof(double);
/*if(length != sizeof(&y)/sizeof(double))
{
throw std::runtime_error(std::string("Vectors must be the same size"));
}*/
for(int i=0; i<length; i++)
{
product += x[i]*y[i];
}
return product;
}
double* crossProduct(double x [], double y [], double product [])
{
//int length = sizeof(&x)/sizeof(double);
//cout << "Cross length " << length << endl;
/*if(length != sizeof(&y)/sizeof(double))
{
throw std::runtime_error(std::string("Vectors must be the same size"));
}
else if(length != 3)
{
//throw std::runtime_error(std::string("Cross Product only works for vectors of size 3"));
}*/
product[0] = x[1]*y[2]-x[2]*y[1];
product[1] = x[2]*y[0]-x[0]*y[2];
product[2] = x[0]*y[1]-x[1]*y[0];
return product;
}
double* add(double x [], double y [], double sum [])
{
//int length = sizeof(&x)/sizeof(double);
/*if(length != sizeof(&y)/sizeof(double))
{
throw std::runtime_error(std::string("Vectors must be the same size"));
}*/
for(int i=0;i<length;i++)
{
sum[i] = x[i]+y[i];
}
return sum;
}
double* subtract(double x [], double y [], double sum [])
{
//int length = sizeof(&x)/sizeof(double);
/*if(length != sizeof(&y)/sizeof(double))
{
throw std::runtime_error(std::string("Vectors must be the same size"));
}*/
for(int i=0;i<length;i++)
{
sum[i] = x[i]-y[i];
}
return sum;
}
double* multiply(double x,double y [], double product [])
{
//int length = sizeof(&y)/sizeof(double);
for(int i=0;i<length;i++)
{
product[i] = y[i]*x;
}
return product;
}
double* normalize(double x [],double normal [])
{
//int length = sizeof(&x)/sizeof(double);
//cout << "normal length: " << length << endl;
double sum = 0;
for(int i=0;i<length;i++)
{
sum += pow(x[i],2);
}
sum = pow(sum,0.5);
for(int i=0;i<length;i++)
{
normal[i] = x[i]/sum;
}
return normal;
}
double magnitude(double x [])
{
double mag = pow(pow(x[0],2)+pow(x[1],2)+pow(x[2],2),0.5);
return mag;
}
void mat_mult3x3(double x [3][3], double y [3][3], double ans [3][3])
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
ans[i][j] = x[i][0]*y[0][j]+x[i][1]*y[1][j]+x[i][2]*y[2][j];
}
}
}
void mat_mult3x1(double x[3][3],double y[3],double ans[3])
{
for(int i=0;i<3;i++)
{
ans[i] = x[i][0]*y[0]+x[i][1]*y[1]+x[i][2]*y[2];
}
}
void transpose3(double x[3][3], double ans[3][3])
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
ans[i][j] = x[j][i];
}
}
}
double determinant3(double x[3][3])
{
double ans = x[0][0]*x[1][1]*x[2][2]+x[0][1]*x[1][2]*x[2][0]+x[0][2]*x[1][0]*x[2][1]-x[0][2]*x[1][1]*x[2][0]-x[0][1]*x[1][0]*x[2][2]-x[0][0]*x[1][2]*x[2][1];
return ans;
}