-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoint.h
78 lines (60 loc) · 1.78 KB
/
Point.h
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
#ifndef PROJECT_KMEAN_POINT_H
#define PROJECT_KMEAN_POINT_H
#include <vector>
#include <fstream>
#include <sstream>
#include <iostream>
#include <cmath>
#include <omp.h>
#include <random>
#include <algorithm>
#include <cfloat>
#define FEATS 3
struct Point{
double coordinates[FEATS]{}; // coordinates
int cluster = -1; // no default cluster
Point(){
to_zero(-1);
}
explicit Point(const double coords[FEATS]) : cluster(-1){
for (int i=0; i<FEATS; i++)
coordinates[i] = coords[i];
}
Point(const Point& p1){
for (int i=0; i<FEATS; i++)
coordinates[i] = p1.coordinates[i];
cluster = p1.cluster;
}
void to_zero(int i){
cluster = i;
for (double & coordinate : coordinates)
coordinate = 0.;
}
double compute_distance(const Point& p) const {
double total_d = 0;
for (int i=0; i<FEATS; i++)
total_d += (this->coordinates[i] - p.coordinates[i]) * (this->coordinates[i] - p.coordinates[i]);
return total_d;
}
void operator+=(const Point& p){
for (int i=0; i<FEATS; i++)
this->coordinates[i] += p.coordinates[i];
}
void operator/=(const int& cardinality){
for (double & coordinate : coordinates)
coordinate /= (double)cardinality;
}
bool operator==(const Point& p) const {
for (int i=0; i<FEATS; i++)
if (this->coordinates[i] != p.coordinates[i])
return false;
return true;
}
double compute_rel_diff(const Point& p) const {
double rel_diff = 0.;
for (int i=0; i<FEATS; i++)
rel_diff += std::abs(this->coordinates[i] - p.coordinates[i]);
return rel_diff;
}
};
#endif //PROJECT_KMEAN_POINT_H