-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector3.cpp
70 lines (57 loc) · 1.29 KB
/
vector3.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
#include "vector3.h"
#include <cmath>
Vector3::Vector3() : x(0), y(0), z(0) {}
Vector3::Vector3(double _x, double _y, double _z)
: x(_x), y(_y), z(_z)
{}
Vector3 Vector3::operator+(const Vector3 &other) const
{
return Vector3(x + other.x, y + other.y, z + other.z);
}
Vector3 Vector3::operator-(const Vector3 &other) const
{
return Vector3(x - other.x, y - other.y, z - other.z);
}
Vector3 Vector3::operator*(const double &scalar) const
{
return Vector3(x*scalar, y*scalar, z*scalar);
}
double Vector3::dot(const Vector3 &other) const
{
return (x*other.x + y*other.y + z*other.z);
}
Vector3 Vector3::cross(const Vector3 &other) const
{
return Vector3(y*other.z - z*other.y,
z*other.x - x*other.z,
x*other.y - y*other.x);
}
Vector3& Vector3::operator+=(const Vector3 &other)
{
x += other.x;
y += other.y;
z += other.z;
return *this;
}
Vector3 Vector3::normal()
{
return (*this)*(1/sqrt(dot(*this)));
}
double Vector3::magSquare() const
{
return dot(*this);
}
double Vector3::magnitude() const
{
return sqrt(magSquare());
}
std::ostream& operator<<(std::ostream& stream, Vector3 vec)
{
stream << "<" << vec.x <<"," << vec.y <<","<<vec.z<<">";
return stream;
}
std::istream& operator>>(std::istream& stream, Vector3& vec)
{
stream >> vec.x >> vec.y >> vec.z;
return stream;
}