forked from bl4ckb0ne/delaunay-triangulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
105 lines (84 loc) · 2.39 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
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <array>
#include <SFML/Graphics.hpp>
#include "vector2.h"
#include "triangle.h"
#include "delaunay.h"
float RandomFloat(float a, float b) {
const float random = ((float) rand()) / (float) RAND_MAX;
const float diff = b - a;
const float r = random * diff;
return a + r;
}
int main(int argc, char * argv[])
{
int numberPoints = 40;
if (argc==1)
{
numberPoints = (int) roundf(RandomFloat(4, numberPoints));
}
else if (argc>1)
{
numberPoints = atoi(argv[1]);
}
srand (time(NULL));
std::cout << "Generating " << numberPoints << " random points" << std::endl;
std::vector<Vector2<float> > points;
for(int i = 0; i < numberPoints; ++i) {
points.push_back(Vector2<float>(RandomFloat(0, 800), RandomFloat(0, 600)));
}
Delaunay<float> triangulation;
const std::vector<Triangle<float> > triangles = triangulation.triangulate(points);
std::cout << triangles.size() << " triangles generated\n";
const std::vector<Edge<float> > edges = triangulation.getEdges();
std::cout << " ========= ";
std::cout << "\nPoints : " << points.size() << std::endl;
for(const auto &p : points)
std::cout << p << std::endl;
std::cout << "\nTriangles : " << triangles.size() << std::endl;
for(const auto &t : triangles)
std::cout << t << std::endl;
std::cout << "\nEdges : " << edges.size() << std::endl;
for(const auto &e : edges)
std::cout << e << std::endl;
// SFML window
sf::RenderWindow window(sf::VideoMode(800, 600), "Delaunay triangulation");
// Transform each points of each vector as a rectangle
std::vector<sf::RectangleShape*> squares;
for(const auto p : points) {
sf::RectangleShape *c1 = new sf::RectangleShape(sf::Vector2f(4, 4));
c1->setPosition(p.x, p.y);
squares.push_back(c1);
}
// Make the lines
std::vector<std::array<sf::Vertex, 2> > lines;
for(const auto &e : edges) {
lines.push_back({{
sf::Vertex(sf::Vector2f(e.p1.x + 2, e.p1.y + 2)),
sf::Vertex(sf::Vector2f(e.p2.x + 2, e.p2.y + 2))
}});
}
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
// Draw the squares
for(const auto &s : squares) {
window.draw(*s);
}
// Draw the lines
for(const auto &l : lines) {
window.draw(l.data(), 2, sf::Lines);
}
window.display();
}
return 0;
}