-
Notifications
You must be signed in to change notification settings - Fork 0
/
point.cpp
133 lines (120 loc) · 2.47 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
#include "point.h"
#include <cmath>
#include <iosfwd>
Point::Point() {}
Point::Point(unsigned int dimension) :
m_dim(dimension)
{
m_coord = std::vector<double>(m_dim,0);
}
Point::Point(const Point& p)
{
m_dim = p.m_dim;
m_coord = p.m_coord;
}
double Point::dist(Point const & p1, Point const & p2)
{
assert(p1.get_dimension() == p2.get_dimension());
unsigned int dim = p1.get_dimension();
double res = 0;
double summand = 0;
for(unsigned int i = 0; i< dim; i++)
{
summand = (p1[i]-p2[i]);
summand *= summand;
res += summand;
}
return sqrt(res);
}
double Point::max_norm_dist(const Point &p1, const Point & p2)
{
assert(p1.get_dimension() == p2.get_dimension());
unsigned int dim = p1.get_dimension();
double max = 0;
double diff = 0;
for (unsigned int i = 0; i< dim; i++)
{
diff = p1[i]-p2[i];
diff = (diff>0)?diff:-diff;
if (diff > max)
{
max = diff;
}
}
return max;
}
const double & Point::operator[](unsigned int coord) const
{
#if DEBUG
assert(coord < m_dim);
assert(m_coord.size() > coord);
#endif
return m_coord[coord];
}
double & Point::operator[](unsigned int coord)
{
#if DEBUG
assert(coord < m_dim);
assert(m_coord.size() > coord);
#endif
return m_coord[coord];
}
Point Point::operator+(const Point& p1) const
{
#if DEBUG
assert(m_dim == p1.m_dim);
#endif
Point res(m_dim);
for(int i = 0; i<m_dim; i++)
{
res.m_coord[i] = m_coord[i] + p1.m_coord[i];
}
return res;
}
Point Point::operator-(const Point& p1) const
{
#if DEBUG
assert(m_dim == p1.m_dim);
#endif
Point res(m_dim);
for(int i = 0; i<m_dim; i++)
{
res.m_coord[i] = m_coord[i] - p1.m_coord[i];
}
return res;
}
bool Point::operator==(const Point &p) const
{
if (p.m_dim != m_dim) {
return false;
}
for (int i = 0; i<m_dim; i++) {
if (m_coord[i] != p.m_coord[i]) {
return false;
}
}
return true;
}
unsigned int Point::get_dimension() const
{
return m_dim;
}
std::ostream &operator<<( std::ostream &output, const Point &p )
{
if(p.m_dim == 0) return output;
output << p.m_coord[0];
for(int i = 1; i<p.m_dim; i++)
{
output << " " << p.m_coord[i];
}
return output;
}
Point Point::operator*(double a) const
{
Point res(*this);
for(int i = 0; i<m_dim; i++)
{
res[i] *= a;
}
return res;
}