This repository was archived by the owner on Sep 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.cpp
More file actions
151 lines (128 loc) · 3.33 KB
/
Copy pathVector.cpp
File metadata and controls
151 lines (128 loc) · 3.33 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*----------------------------------------------------------
* 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);
}
Vector Vector::rotation(float theta, Vector axis)
{
Vector rotated = Vector();
theta = theta * 3.14159265359 / 180.0;
if (axis.x == 1)
{
rotated.x = x;
rotated.y = y * cos(theta) - z * sin(theta);
rotated.z = y * sin(theta) + z * cos(theta);
}
else if (axis.y == 1)
{
rotated.x = x * cos(theta) + z * sin(theta);
rotated.y = y;
rotated.z = -x * sin(theta) + z * cos(theta);
}
else if (axis.z == 1)
{
rotated.x = x * cos(theta) - y * sin(theta);
rotated.y = x * sin(theta) + y * cos(theta);
rotated.z = z;
}
return rotated;
}
// 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());
}