-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
220 lines (187 loc) · 5.69 KB
/
main.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <iostream>
#include <vector>
#include <ctime>
#include <cmath>
#include <GL/freeglut.h>
#include "MinimumSpanningTree.h"
#include "GraphEdge.h"
#include "DelaunayTriangulation.h"
#include "Point.hpp"
using namespace std;
vector<Point> points;
vector<GraphEdge<Point, double>> mst;
Edges edges;
int XX;
void display() {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // BackGround color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the color buffer (background)
// Anti - aliasing Begin Сглаживание
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH, GL_NICEST);
glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH, GL_NICEST);
// Anti - aliasing End
double SIZE = (XX / 7.0) * 4.5;
double T = 0.75;
// Draw triangulations
glLineWidth(1.5);
glColor3f(0.859f, 0.859f, 0.859f);
glBegin(GL_LINES);
for (const auto& item : edges) {
const Point& from = item.first;
for (const Point& to : item.second) {
glVertex2f(from.getX() / SIZE - T, from.getY() / SIZE - T);
glVertex2f(to.getX() / SIZE - T, to.getY() / SIZE - T);
}
}
glEnd();
// Draw mst
glLineWidth(3.0);
glColor3f(0.0f, 0.69f, 0.0f);
glBegin(GL_LINES);
for (const GraphEdge<Point, double>& edge : mst) {
glVertex2f(edge.getFrom().getX() / SIZE - T, edge.getFrom().getY() / SIZE - T);
glVertex2f(edge.getTo().getX() / SIZE - T, edge.getTo().getY() / SIZE - T);
}
glEnd();
// Draw points
glPointSize(10.0);
glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_POINTS);
for (const Point& point : points) {
glVertex2f(point.getX() / SIZE - T, point.getY() / SIZE - T);
}
glEnd();
glutSwapBuffers(); // Swap the front and back frame buffers (double buffering)
}
void draw(const vector<Point>& points, const Edges& edges, const vector<GraphEdge<Point, double>>& mst) {
::points = points;
::edges = edges;
::mst = mst;
glutDisplayFunc(display);
}
void generatePoints(int sz, vector<Point>& points) {
points.clear();
set<Point> set_points;
srand(time(nullptr));
int t = sz / 3;
XX = t;
for (int i = 0; i < sz; ++i) {
double x = rand() % t;
double y = rand() % t;
for (int j = 0; j < 5; ++j) {
if (set_points.find({x, y}) != set_points.end()) {
x = rand() % t;
y = rand() % t;
} else break;
}
set_points.insert({x, y});
}
points = {set_points.begin(), set_points.end()};
}
void Do(int argc, char **argv, int chose) {
vector<Point> points = { // TODO: Work incorrect (vertical line)
{0, 1},
{1, 0},
{1, 2},
{1, 3},
{2, 1},
{3, 3},
{4, 2},
{5, 0},
{5, 1},
{5, 3},
{2, 5},
{7, 6},
{5, 8},
{0, 6},
{6, 6},
{5, 5},
{3, 0},
{4, 9},
{4, 0},
{3, 4},
};
// vector<Point> points = {
// {0, 1}, {1, 0}, {1, 2}, {1, 3}, {2, 1},
// {3, 3},
//
// {2, 5}, {0, 6},
//
// {3, 0}, {3, 4},
// };
// vector<Point> points = {
// {4, 2}, {5, 0}, {5, 1}, {5, 3},
// {7, 6}, {5, 8}, {6, 6},
// {5, 5}, {4, 9}, {4, 0},
// };
// vector<Point> points = {
// {0, 1}, {1, 0}, {1, 2}, {1, 3}, {2, 1},
// {3, 3}, {3, 0}, {3, 4},
// };
if (chose == 2) {
int q;
cout << "Enter count of points:\n";
cin >> q;
generatePoints(q, points);
} else if (chose == 1) {
cout << "Enter count of points and points after that:\n";
int q;
cin >> q;
points.clear();
set<Point> set_points;
XX = 1;
for (int i = 0; i < q; ++i) {
double xx, yy;
cin >> xx >> yy;
XX = max(XX, (int) abs(xx));
XX = max(XX, (int) abs(yy));
set_points.insert({xx, yy});
}
points = {set_points.begin(), set_points.end()};
}
DelaunayTriangulation delaunayTriangulation = DelaunayTriangulation();
Edges edges = delaunayTriangulation.getTriangulation(points);
MinimumSpanningTree<Point> minimumSpanningTree;
vector<GraphEdge<Point, double>> graphEdges;
for (const auto& item : edges) {
const Point& from = item.first;
for (const Point& to : item.second) {
graphEdges.emplace_back(from, to, from.distToPoint(to));
}
}
vector<GraphEdge<Point, double>> mst;
double length = 0.0;
pair<vector<GraphEdge<Point, double>>, double> res = minimumSpanningTree.getMST(graphEdges);
mst = move(res.first);
length = res.second;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA | GLUT_MULTISAMPLE);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Delaunay triangulations");
draw(points, edges, mst);
glutMainLoop();
}
int main(int argc, char **argv) {
cout << "Please, chose what to do:\n"
"1. Read points from file\n"
"2. Generate points\n";
int chose = -1;
cin >> chose;
switch (chose) {
case 1:
Do(argc, argv, 1);
break;
case 2:
Do(argc, argv, 2);
break;
default:
cout << "WRONG";
}
return 0;
}