-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementation.cpp
More file actions
91 lines (83 loc) · 2.94 KB
/
Implementation.cpp
File metadata and controls
91 lines (83 loc) · 2.94 KB
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
#include "Implementation.hpp"
/*------------------------------------------------------------------------------*/
/* Point */
double Point::euclideanDistance(Point other) {
return sqrt((x - other.x) * (x - other.x) + (y - other.y) * (y - other.y));
};
/*------------------------------------------------------------------------------*/
/* KMeansData */
void KMeansData::setRandomCentroidsArray(int num_centroids, double min, double max, bool verbose) {
num_arr_points = num_centroids;
std::srand(std::time(NULL));
centroids = new Centroid*[num_centroids + 1];
int i;
for (i = 0 ; i < num_centroids ; i++) {
Centroid* centroid = new Centroid(i, randNum(min, max), randNum(min, max));
centroids[i] = centroid;
}
centroids[i] = nullptr;
if (verbose) {
std::cout << "Centroids: " << "\n";
for (int i = 0 ; i < num_centroids ; i++) {
std::cout << centroids[i]->toString() << std::endl;
}
}
};
void KMeansData::setRandomCentroidsVector(int num_centroids, double min, double max, bool verbose) {
num_vec_points = num_centroids;
std::srand(std::time(NULL));
for (int i = 0 ; i < num_centroids ; i++) {
Centroid centroid(i, randNum(min, max), randNum(min, max));
centroids_vec.push_back(centroid);
}
if (verbose) {
std::cout << "Centroids: " << "\n";
for (int i = 0 ; i < num_centroids ; i++) {
std::cout << centroids[i]->toString() << std::endl;
}
}
};
KMeansData::~KMeansData() {
if (num_arr_points > 0) {
for (int i = 0 ; i < num_arr_points ; i++) { //deallocate centroids array
delete centroids[i];
}
delete [] centroids;
}
};
/*------------------------------------------------------------------------------*/
/* Helper Functions */
// Centroid** getCentroidsArray(int num_centroids, double min, double max, bool verbose) {
// KMeansData dataFactory(num_centroids);
// dataFactory.setRandomCentroidsArray(num_centroids, min, max);
// if (verbose) {
// std::cout << "Centroids: " << "\n";
// for (int i = 0 ; i < num_centroids ; i++) {
// std::cout << dataFactory.getCentroidsArray()[i]->toString() << std::endl;
// }
// }
// return dataFactory.getCentroidsArray();
// }
//
// std::vector<Centroid> getCentroidsVector(int num_centroids, double min, double max, bool verbose) {
// KMeansData dataFactory(num_centroids);
// dataFactory.setRandomCentroidsVector(num_centroids, min, max);
// if (verbose) {
// std::cout << "Centroids: " << "\n";
// for (int i = 0 ; i < num_centroids ; i++) {
// std::cout << dataFactory.getCentroidsVector()[i].toString() << std::endl;
// }
// }
// return dataFactory.getCentroidsVector();
// }
Point getPoint(double min, double max, bool verbose) {
Point p(randNum(min, max), randNum(min, max));
if (verbose) {
std::cout << "Point: " << std::endl << p.toString() << std::endl;
}
return p;
}
double randNum(double min, double max) {
double n = (double) rand() / RAND_MAX;
return min + n * (max - min);
};