-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint.cpp
172 lines (148 loc) · 2.83 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
/* point.cpp
Implementation of Point class
Written by Matt Sachtler for CS225 MP6
Spring 2009
Revision history:
3/21/2009 Created
*/
template<int Dim>
bool Point<Dim>::enable_mines = false;
/* Point constructor. Initializes everything to 0.
*/
template<int Dim>
Point<Dim>::Point()
: am_mine(false)
{
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])
: am_mine(false)
{
for(int i = 0; i < Dim; ++i)
vals[i] = arr[i];
}
/* Point constructor
Copy the array of points in
set mine flag
*/
template<int Dim>
Point<Dim>::Point(double arr[Dim], bool mine)
: am_mine(mine)
{
for(int i = 0; i < Dim; ++i)
vals[i] = arr[i];
}
template<int Dim>
template <typename T>
Point<Dim>::Point(T x0, T x1, T x2)
: am_mine(false)
{
vals[0] = x0;
vals[1] = x1;
vals[2] = x2;
}
template<int Dim>
template <typename T>
Point<Dim>::Point(T x, ...)
: am_mine(false)
{
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>
double Point<Dim>::operator[](int index) const
{
if (enable_mines && am_mine)
cout << "Hit mine " << *this << endl;
if (index >= Dim)
{
out_of_range e("Point index out of range");
throw e;
}
return vals[index];
}
template<int Dim>
double & Point<Dim>::operator[](int index)
{
if (enable_mines && am_mine)
cout << "Hit mine " << *this << endl;
if (index >= Dim)
{
out_of_range e("Point index out of range");
throw e;
}
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 << (am_mine ? '{' : '(');
for (int i = 0; i < Dim - 1; ++i)
out << vals[i] << ", ";
out << vals[Dim - 1];
out << (am_mine ? '}' : ')');
}
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);
}