-
Notifications
You must be signed in to change notification settings - Fork 0
/
point.cpp
executable file
·156 lines (132 loc) · 3.18 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
/* point.cpp
Implementation of Point class
@author Wade Fagen-Ulmschneider <waf@illinois.edu>
@date Fall 2017
@note Based on Matt Sachtler's MP6 Point class.
*/
/* Point constructor. Initializes everything to 0.
*/
template <int Dim>
Point<Dim>::Point() {
for (int i = 0; i < Dim; ++i)
vals[i] = 0;
}
/* Point constructor
Copy the array of points in
*/
template <int Dim>
Point<Dim>::Point(double arr[Dim]) {
for (int i = 0; i < Dim; ++i)
vals[i] = arr[i];
}
/*
* Point constructor with mines, used for grading.
*/
template <int Dim>
Point<Dim>::Point(double arr[Dim], bool isMine, const MineAction * mineAction) : Point<Dim>(arr) {
isMine_ = isMine;
mineAction_ = mineAction;
}
template <int Dim>
template <typename T>
Point<Dim>::Point(T x0, T x1, T x2) {
vals[0] = x0;
vals[1] = x1;
vals[2] = x2;
}
template <int Dim>
template <typename T>
Point<Dim>::Point(T x, ...) {
vals[0] = x;
va_list ap;
va_start(ap, x);
for (int i = 1; i < Dim; i++)
vals[i] = va_arg(ap, T);
va_end(ap);
}
template <int Dim>
Point<Dim>& Point<Dim>::operator=(const Point<Dim>& other)
{
for (int i = 0; i < Dim; i++) {
vals[i] = other.vals[i];
}
isMine_ = other.isMine_;
mineAction_ = other.mineAction_;
return *this;
}
template <int Dim>
double Point<Dim>::operator[](int index) const {
if (index >= Dim) {
out_of_range e("Point index out of range");
throw e;
}
if (isMine_) { mineAction_->onMine(*this); }
return vals[index];
}
template <int Dim>
double& Point<Dim>::operator[](int index) {
if (index >= Dim) {
out_of_range e("Point index out of range");
throw e;
}
if (isMine_) { mineAction_->onMine(*this); }
return vals[index];
}
template <int Dim>
void Point<Dim>::set(int index, double val) {
if (index >= Dim) {
out_of_range e("Point index out of range");
throw e;
}
vals[index] = val;
}
template <int Dim>
void Point<Dim>::print(std::ostream& out /* = cout */) const {
out << '(';
for (int i = 0; i < Dim - 1; ++i)
out << vals[i] << ", ";
out << vals[Dim - 1];
out << ')';
}
template <int Dim>
std::ostream& operator<<(std::ostream& out, const Point<Dim>& p) {
p.print(out);
return out;
}
template <int Dim>
bool Point<Dim>::operator==(const Point<Dim> p) const {
return !(*this != p);
}
template <int Dim>
bool Point<Dim>::operator!=(const Point<Dim> p) const {
bool eq = true;
for (int i = 0; i < Dim; ++i)
eq &= (vals[i] == p.vals[i]);
return !eq;
}
template <int Dim>
bool Point<Dim>::operator<(const Point<Dim> p) const {
bool less = false;
for (int i = 0; i < Dim; ++i) {
less = vals[i] < p[i];
if (vals[i] != p[i])
break;
}
return less;
}
template <int Dim>
bool Point<Dim>::operator<=(const Point<Dim> p) const {
return (*this < p) || (*this == p);
}
template <int Dim>
bool Point<Dim>::operator>(const Point<Dim> p) const {
return !(*this < p);
}
template <int Dim>
bool Point<Dim>::operator>=(const Point<Dim> p) const {
return (*this > p) || (*this == p);
}
template <int Dim>
bool Point<Dim>::isMine() const {
return isMine_;
}