-
Notifications
You must be signed in to change notification settings - Fork 1
/
mkgraph.cpp
109 lines (91 loc) · 3.25 KB
/
mkgraph.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
#include <stdexcept>
#include <random>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include "help.h"
using namespace std;
random_device rd;
default_random_engine rng(rd());
class Point {
public:
Point(float x = 0, float y = 0) : x(x), y(y) {}
float x;
float y;
};
ostream& operator<<(ostream& out, const Point& point) {
out << fixed << setprecision(4) << point.x << " " << point.y;
return out;
}
void buildNormalCluster(const char *spec, vector<Point>& points) {
stringstream ss(spec);
int numPoints; ss >> numPoints; ss.ignore();
float meanX; ss >> meanX; ss.ignore();
float meanY; ss >> meanY; ss.ignore();
float deviationX; ss >> deviationX; ss.ignore();
float deviationY; ss >> deviationY;
if (ss.fail() || !ss.eof())
throw runtime_error("-n: Malformed spec\n" + normalHelp);
normal_distribution<float> distributionX(meanX, deviationX);
normal_distribution<float> distributionY(meanY, deviationY);
for (int i = 0; i < numPoints; ++i) {
points.emplace_back(distributionX(rng), distributionY(rng));
}
}
void buildUniformCluster(const char *spec, vector<Point>& points) {
stringstream ss(spec);
int numPoints; ss >> numPoints; ss.ignore();
float x; ss >> x; ss.ignore();
float y; ss >> y; ss.ignore();
float width; ss >> width; ss.ignore();
float height; ss >> height;
if (ss.fail() || !ss.eof())
throw runtime_error("-u: Malformed spec\n" + normalHelp);
uniform_real_distribution<float> distributionX(x, x + width);
uniform_real_distribution<float> distributionY(y, y + height);
for (int i = 0; i < numPoints; ++i) {
points.emplace_back(distributionX(rng), distributionY(rng));
}
}
int main(int argc, char *argv[]) {
vector<Point> points;
if (argc == 1) {
buildNormalCluster("1000,0,0,200000,200000", points);
} else {
try {
for (int i = 1; i < argc; ++i) {
string option(argv[i]);
if (option.compare("-h") == 0) {
cout << generalHelp << endl;
return 0;
} else if (option.compare("-hn") == 0) {
cout << normalHelp << endl;
return 0;
} else if (option.compare("-hu") == 0) {
cout << uniformHelp << endl;
return 0;
} else if (option.compare("-n") == 0) {
if (i + 1 == argc)
throw runtime_error("-n: Missing arg\n" + normalHelp);
buildNormalCluster(argv[i + 1], points);
++i;
} else if (option.compare("-u") == 0) {
if (i + 1 == argc)
throw runtime_error("-u: Missing arg\n" + uniformHelp);
buildUniformCluster(argv[i + 1], points);
++i;
} else {
throw runtime_error("Unknown option '" + option + "'");
}
}
} catch (exception& e) {
cerr << e.what() << endl;
return 1;
}
}
std::cout << points.size() << std::endl;
for (const Point& point : points)
std::cout << point << std::endl;
return 0;
}