-
Notifications
You must be signed in to change notification settings - Fork 1
/
example-line-segment-intersections.cpp
101 lines (89 loc) · 2.3 KB
/
example-line-segment-intersections.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
#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), "Line Segment Intersections");
vector<LineSegment> ls;
vector<Point> endpoints;
bool lineState = false;
Point begin, end;
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) {
float x = event.mouseButton.x;
float y = event.mouseButton.y;
if (lineState) {
lineState = false;
ls.push_back(LineSegment(begin, end));
end = Point(x, y);
endpoints.push_back(end);
} else {
lineState = true;
begin = Point(x, y);
end = Point(x, y);
endpoints.push_back(begin);
}
}
}
if (event.type == sf::Event::MouseMoved) {
float x = event.mouseMove.x;
float y = event.mouseMove.y;
end = Point(x, y);
}
}
window.clear(sf::Color::White);
sf::CircleShape shape(RADIUS);
shape.setFillColor(sf::Color::Black);
if (lineState) {
sf::Vertex line[] =
{
sf::Vertex(sf::Vector2f(begin.x, begin.y)),
sf::Vertex(sf::Vector2f(end.x, end.y)),
};
line[0].color = sf::Color::Black;
line[1].color = sf::Color::Black;
window.draw(line, 2, sf::Lines);
}
for (auto p: endpoints) {
shape.setPosition(p.x-RADIUS, p.y-RADIUS);
window.draw(shape);
}
for (auto l: ls) {
sf::Vertex line[] =
{
sf::Vertex(sf::Vector2f(l.begin.x, l.begin.y)),
sf::Vertex(sf::Vector2f(l.end.x, l.end.y)),
};
line[0].color = sf::Color::Black;
line[1].color = sf::Color::Black;
window.draw(line, 2, sf::Lines);
}
for (int i = 0; i < ls.size(); ++i) {
for (int j = i+1; j < ls.size(); ++j) {
//std::cout<<"here 0\n";
if (ls[i].intersects(ls[j])) {
//std::cout << "here\n";
Point p = ls[i].intersection(ls[j]);
shape.setPosition(p.x-RADIUS, p.y-RADIUS);
shape.setFillColor(sf::Color::Red);
window.draw(shape);
}
}
}
window.display();
}
return 0;
}