-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCartesian3.cpp
More file actions
157 lines (138 loc) · 4.63 KB
/
Cartesian3.cpp
File metadata and controls
157 lines (138 loc) · 4.63 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
151
152
153
154
155
156
157
//////////////////////////////////////////////////////////////////////
//
// University of Leeds
// COMP 5812M Foundations of Modelling & Rendering
// User Interface for Coursework
//
// September, 2020
//
// ------------------------
// Cartesian3.h
// ------------------------
//
// A minimal class for a point in Cartesian space
//
///////////////////////////////////////////////////
#include "Cartesian3.h"
#include "math.h"
#include <iomanip>
// constructors
Cartesian3::Cartesian3()
: x(0.0), y(0.0), z(0.0)
{}
Cartesian3::Cartesian3(float X, float Y, float Z)
: x(X), y(Y), z(Z)
{}
// equality operator
bool Cartesian3::operator ==(const Cartesian3 &other) const
{ // Cartesian3::operator ==()
return ((x == other.x) && (y == other.y) && (z == other.z));
} // Cartesian3::operator ==()
// unary minus operator
Cartesian3 Cartesian3::operator-() const
{ // Cartesian3::operator-()
Cartesian3 returnVal(-x, -y, -z);
return returnVal;
} // Cartesian3::operator-()
// addition operator
Cartesian3 Cartesian3::operator +(const Cartesian3 &other) const
{ // Cartesian3::operator +()
Cartesian3 returnVal(x + other.x, y + other.y, z + other.z);
return returnVal;
} // Cartesian3::operator +()
// subtraction operator
Cartesian3 Cartesian3::operator -(const Cartesian3 &other) const
{ // Cartesian3::operator -()
Cartesian3 returnVal(x - other.x, y - other.y, z - other.z);
return returnVal;
} // Cartesian3::operator -()
// multiplication operator
Cartesian3 Cartesian3::operator *(float factor) const
{ // Cartesian3::operator *()
Cartesian3 returnVal(x * factor, y * factor, z * factor);
return returnVal;
} // Cartesian3::operator *()
// division operator
Cartesian3 Cartesian3::operator /(float factor) const
{ // Cartesian3::operator /()
Cartesian3 returnVal(x / factor, y / factor, z / factor);
return returnVal;
} // Cartesian3::operator /()
// dot product routine
float Cartesian3::dot(const Cartesian3 &other) const
{ // Cartesian3::dot()
float returnVal = x * other.x + y * other.y + z * other.z;
return returnVal;
} // Cartesian3::dot()
// cross product routine
Cartesian3 Cartesian3::cross(const Cartesian3 &other) const
{ // Cartesian3::cross()
Cartesian3 returnVal(y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x);
return returnVal;
} // Cartesian3::cross()
// routine to find the length
float Cartesian3::length() const
{ // Cartesian3::length()
return sqrt(x*x + y*y + z*z);
} // Cartesian3::length()
// normalisation routine
Cartesian3 Cartesian3::unit() const
{ // Cartesian3::unit()
float length = sqrt(x*x+y*y+z*z);
Cartesian3 returnVal(x/length, y/length, z/length);
return returnVal;
} // Cartesian3::unit()
// operator that allows us to use array indexing instead of variable names
float &Cartesian3::operator [] (const int index)
{ // operator []
// use default to catch out of range indices
// we could throw an exception, but will just return the 0th element instead
switch (index)
{ // switch on index
case 0:
return x;
case 1:
return y;
case 2:
return z;
// actually the error case
default:
return x;
} // switch on index
} // operator []
// operator that allows us to use array indexing instead of variable names
const float &Cartesian3::operator [] (const int index) const
{ // operator []
// use default to catch out of range indices
// we could throw an exception, but will just return the 0th element instead
switch (index)
{ // switch on index
case 0:
return x;
case 1:
return y;
case 2:
return z;
// actually the error case
default:
return x;
} // switch on index
} // operator []
// multiplication operator
Cartesian3 operator *(float factor, const Cartesian3 &right)
{ // operator *
// scalar multiplication is commutative, so flip & return
return right * factor;
} // operator *
// stream input
std::istream & operator >> (std::istream &inStream, Cartesian3 &value)
{ // stream output
inStream >> value.x >> value.y >> value.z;
return inStream;
} // stream output
// stream output
std::ostream & operator << (std::ostream &outStream, const Cartesian3 &value)
{ // stream output
outStream << std::setprecision(4) << value.x << " " << std::setprecision(4) << value.y << " " << std::setprecision(4) << value.z;
return outStream;
} // stream output