-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector3d.cpp
More file actions
46 lines (34 loc) · 1.08 KB
/
Vector3d.cpp
File metadata and controls
46 lines (34 loc) · 1.08 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
#include "Vector3d.hpp"
#include <cmath>
Vector3d::Vector3d() : x(0), y(0), z(0) {}
Vector3d::Vector3d(double x, double y, double z) : x(x), y(y), z(z) { }
Vector3d Vector3d::zero() {
return Vector3d();
}
double Vector3d::dot(const Vector3d &a) {
return x * a.x + y * a.y + z * a.z;
}
Vector3d Vector3d::cross(const Vector3d &a) {
return Vector3d(y * a.z - z * a.y, z * a.x - x * a.z, x * a.y - y * a.x);
}
Vector3d Vector3d::operator+(const Vector3d &a) {
return Vector3d(x + a.x, y + a.y, z + a.z);
}
Vector3d Vector3d::operator-(const Vector3d &a) {
return Vector3d(x - a.x, y - a.y, z - a.z);
}
Vector3d Vector3d::operator*(const double a) {
return Vector3d(a * x, a * y, a * z);
}
Vector3d Vector3d::operator/(const double a) {
return (*this) * (1 / a);
}
bool Vector3d::operator==(const Vector3d& a) {
return (x == a.x) && (y == a.y) && (z == a.z);
}
double Vector3d::norm() {
return sqrt(x * x + y * y + z * z);
}
std::ostream& operator<<(std::ostream &s, const Vector3d &vec) {
return s << "(" << vec.x << ", " << vec.y << ", " << vec.z << ")";
}