-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample-convex-hull.cpp
More file actions
66 lines (54 loc) · 1.49 KB
/
example-convex-hull.cpp
File metadata and controls
66 lines (54 loc) · 1.49 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
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
#include "Graphics.h"
const float RADIUS = 3.f;
using namespace CG;
using std::vector;
int main()
{
sf::RenderWindow window(sf::VideoMode(1280, 720), "Convex Hull");
vector<Point> points;
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) window.close();
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Q) {
window.close();
}
}
if (event.type == sf::Event::MouseButtonPressed) {
if (event.mouseButton.button == sf::Mouse::Left) {
std::cout << "the left button was pressed\n";
float x = event.mouseButton.x;
float y = event.mouseButton.y;
std::cout << "mouse x: " << x
<< " mouse y: " << y
<< "\n";
Point p(x, y);
if (find(points.begin(), points.end(), p) == points.end())
points.push_back(p);
}
}
}
window.clear(sf::Color::White);
Polygon convex_hull = ConvexHull(points);
sf::ConvexShape conv;
conv.setFillColor(sf::Color::Green);
conv.setPointCount(convex_hull.points.size());
for (int i = 0; i < convex_hull.points.size(); ++i) {
Point p = convex_hull.points[i];
conv.setPoint(i, sf::Vector2f(p.x, p.y));
}
window.draw(conv);
sf::CircleShape shape(RADIUS);
shape.setFillColor(sf::Color::Black);
for (auto p: points) {
shape.setPosition(p.x-RADIUS, p.y-RADIUS);
window.draw(shape);
}
window.display();
}
return 0;
}