-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.cpp
124 lines (104 loc) · 2.91 KB
/
Vector.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*----------------------------------------------------------
* COSC363 Ray Tracer
*
* The vector class
* Use this class for defining vectors and points
* and for performing operations on them.
-------------------------------------------------------------*/
#include <math.h>
#include "Vector.h"
// Default constructor, initialises all values to 0.
Vector::Vector() : x(0), y(0), z(0) {}
// Constructor that takes initial x, y, z values.
Vector::Vector(float a_x, float a_y, float a_z)
: x(a_x), y(a_y), z(a_z) {}
// Scales the input vector by scale
const Vector& Vector::operator*=(float scale)
{
x *= scale;
y *= scale;
z *= scale;
return *this;
}
// Returns a new vector, containing this vector scaled by the input scale factor
Vector Vector::operator*(float scale) const
{
return Vector(*this) *= scale;
}
// Divides this vector by the input scale
const Vector& Vector::operator/=(float scale)
{
return operator*=(1.0f / scale);
}
// Returns a new vector containing the value of this vector divide by the given scale
Vector Vector::operator/(float scale) const
{
return Vector(*this) *= 1.0f / scale;
}
// Add the given vector to this one.
const Vector& Vector::operator+=(const Vector rhs)
{
x += rhs.x;
y += rhs.y;
z += rhs.z;
return *this;
}
// Returns a new vector containing the result of adding the given vector to this one.
Vector Vector::operator+(const Vector rhs) const
{
return Vector(x + rhs.x, y + rhs.y, z + rhs.z);
}
// Subtracts the given vector from this one.
const Vector& Vector::operator-=(const Vector rhs)
{
x -= rhs.x;
y -= rhs.y;
z -= rhs.z;
return *this;
}
// Returns a new vector containing the result of subtract rhs from this one.
Vector Vector::operator-(const Vector rhs) const
{
return Vector(x -rhs.x, y - rhs.y, z - rhs.z);
}
// Compares two vectors
const bool Vector::operator<(const Vector rhs)
{
float l1 = length();
float l2 = rhs.length();
return (l1 < l2);
}
// Scales the current vector by the given scale factor.
void Vector::scale(float scale)
{
operator*=(scale);
}
// Returns the cross product of this vector and rhs.
Vector Vector::cross(const Vector rhs) const
{
float tx = y * rhs.z - z * rhs.y;
float ty = z * rhs.x - x * rhs.z;
float tz = x * rhs.y - y * rhs.x;
return Vector(tx, ty, tz);
}
// Gives the dot product of this vector and rhs
float Vector::dot(const Vector rhs) const
{
return x*rhs.x + y*rhs.y + z*rhs.z;
}
// Gives the distance between this point and rhs
float Vector::dist(const Vector rhs) const
{
Vector vdif = (*this)-rhs;
return vdif.length();
}
// Returns the length of this vector
float Vector::length() const
{
return sqrt(dot(*this));
}
//Normalises this vector.
void Vector::normalise()
{
scale(1.0f / length());
}