-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv3.cpp
More file actions
87 lines (64 loc) · 1.6 KB
/
v3.cpp
File metadata and controls
87 lines (64 loc) · 1.6 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <cmath>
using namespace std;
class Vec3{
public:
float x;
float y;
float z;
Vec3(float a=0, float b=0, float c=0){
x = a;
y = b;
z = c;
}
float mag() const {
return sqrt(x*x + y*y + z*z);
}
Vec3 operator+( const Vec3& B) const {
return Vec3(x+B.x, y+B.y, z+B.z);
}
Vec3 operator-( const Vec3& B) const {
return Vec3(x-B.x, y-B.y, z-B.z);
}
Vec3 operator*( const Vec3& B ) const {
return Vec3( y*B.z - z*B.y , z*B.x - x*B.z , x*B.y - y*B.x );
}
Vec3 operator*( float k ) const {
return Vec3( x*k, y*k, z*k );
}
Vec3 operator/( float k ) const {
return Vec3( x/k, y/k, z/k );
}
float dot( const Vec3& B ) const {
return x*B.x + y*B.y + z*B.z;
}
Vec3 cross( const Vec3& B ) const {
return (*this) * B;
}
friend ostream& operator<<(ostream& os, const Vec3& v){
os << "[x: " << v.x << ", y: " << v.y << ", z: " << v.z << "]";
return os;
}
friend Vec3 operator*(float k, const Vec3& v){
return v * k;
}
float angle( const Vec3& B ) const{
return acos(dot(B) / (mag() * B.mag()));
}
Vec3 unit() const{
float m = mag();
if (m==0) return Vec3(0,0,0);
return Vec3(x/m,y/m,z/m);
}
Vec3 project( const Vec3& B ) const{
float mag2 = B.mag();
if (mag2 == 0) return Vec3(0,0,0);
return B * dot(B)/(mag2*mag2);
}
};
int main() {
Vec3 A(1,1,0);
Vec3 B(1,0,0);
cout << A.unit();
return 0;
}