-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.cpp
More file actions
68 lines (54 loc) · 1.45 KB
/
vector.cpp
File metadata and controls
68 lines (54 loc) · 1.45 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
#include "vector.hpp"
#include <cmath>
#include <stdexcept>
double Vector::operator*(const Vector &v) const
{
return x * v.x + y * v.y + z * v.z;
}
Vector Vector::operator*(const double &a) const
{
return Vector{x * a, y * a, z * a};
}
Vector Vector::operator/(const double &a) const
{
return Vector{x / a, y / a, z / a};
}
Vector Vector::operator-(const Vector &v) const
{
return Vector{x - v.x, y - v.y, z - v.z};
}
Vector Vector::operator-() const
{
return Vector{-x, -y, -z};
}
bool Vector::operator==(const Vector &v) const
{
return x == v.x and y == v.y and z == v.z;
}
bool are_colinear(const Vector &lhs, const Vector &rhs)
{
return cross_product(lhs, rhs) == Vector{0, 0, 0};
}
Vector cross_product(const Vector &lhs, const Vector &rhs)
{
return Vector{ lhs.y * rhs.z - lhs.z * rhs.y,
lhs.z * rhs.x - lhs.x * rhs.z,
lhs.x * rhs.y - lhs.y * rhs.x };
}
double Vector::amplitude() const
{
return sqrt(x * x + y * y + z * z);
}
Vector Vector::as_unit() const
{
if (x == 0 and y == 0 and z == 0)
throw std::invalid_argument("Cannot compute unitary vector from a nul vector");
auto amplitude = Vector::amplitude();
return Vector{x / amplitude, y / amplitude, z / amplitude};
}
std::ostream& operator<<(std::ostream &out, const Vector &vect)
{
return out << "Vect(" << vect.x << ", "
<< vect.y << ", "
<< vect.z << ")";
}