-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector3.cpp
More file actions
97 lines (80 loc) · 1.5 KB
/
Vector3.cpp
File metadata and controls
97 lines (80 loc) · 1.5 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
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include "Vector3.h"
Vector3::Vector3() : x(0), y(0), z(0)
{
return;
}
Vector3::Vector3(double x, double y, double z) : x(x), y(y), z(z)
{
return;
}
Vector3::Vector3(const Vector4 &v)
{
x = v.x;
y = v.y;
z = v.z;
}
Vector3 Vector3::operator+(const Vector3 &rh)
{
return Vector3(x + rh.x, y + rh.y, z + rh.z);
}
Vector3 Vector3::operator-(const Vector3 &rh)
{
return Vector3(x - rh.x, y - rh.y, z - rh.z);
}
Vector3 Vector3::negate()
{
return Vector3(-x, -y, -z);
}
Vector3 Vector3::scale(double s) const
{
return Vector3(s * x, s * y, s * z);
}
double Vector3::length() const
{
return std::sqrt(x*x + y*y + z*z);
}
Vector3 Vector3::normalize() const
{
return scale(1.0 / length());
}
void Vector3::print(std::string comment) const
{
std::cout << comment << " <" << x << ", " << y << ", " << z << ">" << std::endl;
}
Vector3& Vector3::operator=(const Vector3 &rh)
{
x = rh.x;
y = rh.y;
z = rh.z;
return (*this);
}
Vector3& Vector3::operator-=(const Vector3 &rh)
{
(*this) = (*this) - rh;
return (*this);
}
Vector3& Vector3::operator+=(const Vector3 &rh)
{
(*this) = (*this) + rh;
return (*this);
}
Vector3 Vector3::operator*(const double &rh)
{
return scale(rh);
}
Vector3 Vector3::operator-()
{
return negate();
}
Vector3 cross(const Vector3 &lh, const Vector3 &rh)
{
return Vector3(
lh.y * rh.z - lh.z * rh.y,
lh.z * rh.x - lh.x * rh.z,
lh.x * rh.y - lh.y * rh.x);
}
double dot(const Vector3 &lh, const Vector3 &rh)
{
return lh.x * rh.x + lh.y * rh.y + lh.x * rh.y;
}