-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoint.cpp
199 lines (181 loc) · 5.32 KB
/
Point.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "Point.h"
#include "util.h"
Point::Point(const size_t& numOfCoords)
{
for (size_t coordID = 0; coordID < numOfCoords; ++coordID)
coords.push_back(0.0); // 0.0 is the initial value for all the coordinates
}
/*!
* Copy assignment operator.
* @param right hand side operand
* @param left hand side operand
*/
Point& Point::operator=(const Point& rhs)
{
size_t numOfCoords = this->getNumOfCoords();
size_t numOfCoords_rhs = rhs.getNumOfCoords();
if (numOfCoords == numOfCoords_rhs)
{
for (size_t coordID = 0; coordID < numOfCoords; ++coordID)
this->setCoord(coordID, rhs.getCoord(coordID));
}
return *this;
}
Point::~Point()
{
if (!coords.empty()) coords.clear();
}
void Point::setCoord(const size_t& coordID, const double& coordVal)
{
size_t numOfCoords = this->getNumOfCoords();
if (coordID >= 0 && coordID < numOfCoords)
coords[coordID] = coordVal;
}
/*!
* Returns a specific coordinate of the Point.
* @param coordID the ID of the coordinate to be returned.
* @return the specific coordinate of the Point.
*/
double Point::getCoord(const size_t& coordID) const
{
size_t numOfCoords = this->getNumOfCoords();
if (coordID >= 0 && coordID < numOfCoords)
return coords[coordID];
else
return -1.0; // -1.0 is defined as the error value. TODO: Define specific value as error value that will be used throughout the application
}
/*!
* Returns the number of coordinates of the Point.
* @parnam the number of coordinates of the Point.
*/
size_t Point::getNumOfCoords() const
{
return coords.size();
}
/*!
* Returns the length of the vector representation of the Point.
* @return the length of the vector representation of the Point.
*/
double Point::getLength()
{
size_t numOfCoords = this->getNumOfCoords();
double length = 0.0;
for (size_t coordID = 0; coordID < numOfCoords; ++coordID)
length += this->getCoord(coordID) * this->getCoord(coordID);
return std::sqrt(length);
}
bool Point::operator==(const Point& rhs)
{
size_t numOfCoords = this->getNumOfCoords();
size_t numOfCoord_rhs = rhs.getNumOfCoords();
// first the vectors have to have the same size (or be in the same space)
if (numOfCoords == numOfCoord_rhs)
{
bool same = true;
// check if the Points differ at at least one coordinate
for (size_t coordID = 0; coordID < numOfCoords; ++coordID)
{
double lhsCoord = this->getCoord(coordID);
double rhsCoord = rhs.getCoord(coordID);
bool equal = util::approximatelyEqual(lhsCoord, rhsCoord);
if (!equal)
{
same = false;
break;
}
}
return same;
}
else
return false;
}
Point Point::operator+(const Point& rhs)
{
size_t numOfCoords = this->getNumOfCoords();
size_t numOfCoord_rhs = rhs.getNumOfCoords();
if (numOfCoords == numOfCoord_rhs)
{
Point sumPoint = Point(numOfCoords);
for (size_t coordID = 0; coordID < numOfCoords; ++coordID)
{
double coordVal = this->getCoord(coordID) + rhs.getCoord(coordID);
sumPoint.setCoord(coordID, coordVal);
}
return sumPoint;
}
// TODO: Check what should happen the 2 added points have different dimension
else
{
size_t minNumOfCoords = (numOfCoords < numOfCoord_rhs) ? numOfCoords : numOfCoord_rhs;
Point sumPoint = Point(minNumOfCoords);
return sumPoint;
}
}
Point Point::operator-(const Point& rhs)
{
size_t numOfCoords = this->getNumOfCoords();
size_t numOfCoord_rhs = rhs.getNumOfCoords();
if (numOfCoords == numOfCoord_rhs)
{
Point diffPoint = Point(numOfCoords);
for (size_t coordID = 0; coordID < numOfCoords; ++coordID)
{
double coordVal = this->getCoord(coordID) - rhs.getCoord(coordID);
diffPoint.setCoord(coordID, coordVal);
}
return diffPoint;
}
// TODO: Check what should happen the 2 substracted points have different dimension
else
{
size_t minNumOfCoords = (numOfCoords < numOfCoord_rhs) ? numOfCoords : numOfCoord_rhs;
Point diffPoint = Point(minNumOfCoords);
return diffPoint;
}
}
double Point::operator*(const Point& rhs)
{
size_t numOfCoords = this->getNumOfCoords();
size_t numOfCoord_rhs = rhs.getNumOfCoords();
if (numOfCoords == numOfCoord_rhs)
{
double dotProduct = 0.0;
for (size_t coordID = 0; coordID < numOfCoords; ++coordID)
dotProduct += (this->getCoord(coordID) * rhs.getCoord(coordID));
return dotProduct;
}
// TODO: Check what should happen the 2 multiplied points have different dimension
else
return -1.0;
}
Point& Point::operator+=(const double& coordVal)
{
size_t numOfCoords = this->getNumOfCoords();
for (size_t coordID = 0; coordID < numOfCoords; ++coordID)
this->setCoord(coordID, this->getCoord(coordID) + coordVal);
return *this;
}
Point& Point::operator-=(const double& coordVal)
{
size_t numOfCoords = this->getNumOfCoords();
for (size_t coordID = 0; coordID < numOfCoords; ++coordID)
this->setCoord(coordID, this->getCoord(coordID) - coordVal);
return *this;
}
Point& Point::operator*=(const double& coordVal)
{
size_t numOfCoords = this->getNumOfCoords();
for (size_t coordID = 0; coordID < numOfCoords; ++coordID)
this->setCoord(coordID, this->getCoord(coordID) * coordVal);
return *this;
}
Point& Point::operator/=(const double& coordVal)
{
if (coordVal != 0.0) // ATTENTION: Comparing doubles using !=. TODO: Make function for comparing doubles.
{
size_t numOfCoords = this->getNumOfCoords();
for (size_t coordID = 0; coordID < numOfCoords; ++coordID)
this->setCoord(coordID, this->getCoord(coordID) / coordVal);
}
return *this;
}