-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec.cpp
149 lines (118 loc) · 3.12 KB
/
vec.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
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
#include "vec.h"
template <int n>
double dot(doublen<n> x1, doublen<n> x2) {
double sum = 0;
int i;
for (i = 0; i < n; i++) {
sum += x1.x[i] * x2.x[i];
}
return sum;
}
template <int n>
double norm(doublen<n> x1) {
double sum = 0;
int i;
for (i = 0; i < n; i++) {
sum += x1.x[i] * x1.x[i];
}
return sum;
}
template <int n>
doublen<n> operator-(doublen<n> x1, doublen<n> x2) {
doublen<n> x;
int i;
for (i = 0; i < n; i++) {
x.x[i] = x1.x[i] - x1.x[i];
}
return x;
}
template <int n>
doublen<n> operator+(doublen<n> x1, doublen<n> x2) {
doublen<n> x;
int i;
for (i = 0; i < n; i++) {
x.x[i] = x1.x[i] + x1.x[i];
}
return x;
}
float2 operator+(float2 x1, float2 x2) {
return { x1.x + x2.x,x1.y + x2.y };
}
double dot(double2 x1, double2 x2) {
return x1.x*x2.x + x1.y*x2.y;
}
double norm(double2 x1) {
return x1.x*x1.x + x1.y*x1.y;
}
float norm(float2 x1) {
return x1.x*x1.x + x1.y*x1.y;
}
double vol(double2 x1, double2 x2) {
return x1.x*x2.y - x1.y*x2.x;
}
double2 operator-(double2 x1, double2 x2) {
return { x1.x - x2.x,x1.y - x2.y };
}
double2 operator+(double2 x1, double2 x2) {
return { x1.x + x2.x,x1.y + x2.y };
}
double2 operator*(double s, double2 x) {
return { s*x.x,s*x.y };
}
double2 operator*(double2 x, double s) {
return { s*x.x,s*x.y };
}
double2 operator*(double2 a, double2 b) {
return { a.x*b.x - a.y*b.y,a.x*b.y + a.y*b.x };
}
double2 operator/(double2 a, double2 b) {
return double2{ a.x*b.x + a.y*b.y,-a.x*b.y + a.y*b.x }*(1.0 / (b.x*b.x + b.y*b.y));
}
double dot(double3 x1, double3 x2) {
return x1.x*x2.x + x1.y*x2.y + x1.z*x2.z;
}
double norm(double3 x1) {
return x1.x*x1.x + x1.y*x1.y + x1.z*x1.z;
}
double vol(double3 x1, double3 x2, double3 x3) {
return x1.x*(x2.y*x3.z - x2.z*x3.y) + x1.y*(x2.z*x3.x - x2.x*x3.z) + x1.z*(x2.x*x3.y - x2.y*x3.x);
}
double3 cross(double3 x1, double3 x2) {
return { x1.y*x2.z - x1.z*x2.y ,x1.z*x2.x - x1.x*x2.z ,x1.x*x2.y - x1.y*x2.x };
}
double3 operator-(double3 x1, double3 x2) {
return { x1.x - x2.x,x1.y - x2.y ,x1.z - x2.z };
}
double3 operator+(double3 x1, double3 x2) {
return { x1.x + x2.x,x1.y + x2.y ,x1.z + x2.z };
}
double3 operator*(double3 x, double s) {
return { x.x*s,x.y*s,x.z*s };
}
float dot(float3 x1, float3 x2) {
return x1.x*x2.x + x1.y*x2.y + x1.z*x2.z;
}
float norm(float3 x1) {
return x1.x*x1.x + x1.y*x1.y + x1.z*x1.z;
}
float vol(float3 x1, float3 x2, float3 x3) {
return x1.x*(x2.y*x3.z - x2.z*x3.y) + x1.y*(x2.z*x3.x - x2.x*x3.z) + x1.z*(x2.x*x3.y - x2.y*x3.x);
}
float3 cross(float3 x1, float3 x2) {
return { x1.y*x2.z - x1.z*x2.y ,x1.z*x2.x - x1.x*x2.z ,x1.x*x2.y - x1.y*x2.x };
}
float3 operator-(float3 x1, float3 x2) {
return { x1.x - x2.x,x1.y - x2.y ,x1.z - x2.z };
}
float3 operator+(float3 x1, float3 x2) {
return { x1.x + x2.x,x1.y + x2.y ,x1.z + x2.z };
}
float3 operator*(float3 x, float s) {
return { x.x*s,x.y*s,x.z*s };
}
float3 operator*(float s,float3 x) {
return { x.x*s,x.y*s,x.z*s };
}
float2 operator*(float s, float2 x) {
return { x.x*s,x.y*s };
}