-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector3D.cpp
More file actions
44 lines (33 loc) · 1.09 KB
/
Vector3D.cpp
File metadata and controls
44 lines (33 loc) · 1.09 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
#include <cmath>
#include "Vector3D.h"
const Vector3D Vector3D::zero{ 0.0, 0.0, 0.0 };
Vector3D::Vector3D(double x, double y, double z) {
this->X = x;
this->Y = y;
this->Z = z;
}
Vector3D Vector3D::operator+(const Vector3D& b) const {
return Vector3D(X + b.X, Y + b.Y, Z + b.Z);
}
Vector3D Vector3D::operator-(const Vector3D& b) const {
return Vector3D(X - b.X, Y - b.Y, Z - b.Z);
}
Vector3D Vector3D::operator*(double scalar) const {
return Vector3D(X * scalar, Y * scalar, Z * scalar);
}
Vector3D Vector3D::operator/(double scalar) const {
const double scalar_inv = 1.0 / scalar;
return Vector3D(X * scalar_inv, Y * scalar_inv, Z * scalar_inv);
}
double Vector3D::length() const {
return std::sqrt(X * X + Y * Y + Z * Z);
}
Vector3D Vector3D::get_normalized() const {
return *this / length();
}
Vector3D Vector3D::cross(const Vector3D& a, const Vector3D& b) {
return Vector3D(a.Y * b.Z - a.Z * b.Y, a.Z * b.X - a.X * b.Z, a.X * b.Y - a.Y * b.X);
}
double Vector3D::dot(const Vector3D& a, const Vector3D& b) {
return a.X * b.X + a.Y * b.Y + a.Z * b.Z;
}