forked from mateusvrs/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sweep-line.cpp
146 lines (110 loc) · 3.31 KB
/
sweep-line.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
struct Segment {
double a, b, c;
Point A, B;
size_t idx;
Segment(const Point& P, const Point& Q, size_t i)
: a(P.y - Q.y),
b(Q.x - P.x),
c(P.x * Q.y - Q.x * P.y),
A(P),
B(Q),
idx(i) {}
bool operator<(const Segment& s) const {
return (-a * sweep_x - c) * s.b < (-s.a * sweep_x - s.c) * b;
}
optional<Point> intersection(const Segment& s) const {
auto det = a * s.b - b * s.a;
if (not equals(det, 0.0)) // Concorrentes
{
auto x = (-c * s.b + s.c * b) / det;
auto y = (-s.c * a + c * s.a) / det;
if (min(A.x, B.x) <= x and x <= max(A.x, B.x) and
min(s.A.x, s.B.x) <= x and x <= max(s.A.x, s.B.x)) {
return Point{x, y};
}
}
return {};
}
static double sweep_x;
};
double Segment::sweep_x;
struct Event {
enum Type { OPEN, INTERSECTION, CLOSE };
Point P;
Type type;
size_t i;
bool operator<(const Event& e) const {
if (P != e.P) return e.P < P;
if (type != e.type) return type > e.type;
return i > e.i;
}
};
void add_neighbor_intersections(const Segment& s, const set<Segment>& sl,
set<Point>& ans,
priority_queue<Event>& events) {
// TODO: garantir que a busca identifique unicamente o elemento s,
// através do ajuste fino da variável Segment::sweep_x
auto it = sl.find(s);
if (it != sl.begin()) {
auto L = *prev(it);
auto P = s.intersection(L);
if (P and ans.count(P.value()) == 0) {
events.push(Event{P.value(), Event::INTERSECTION, s.idx});
ans.insert(P.value());
}
}
if (next(it) != sl.end()) {
auto U = *next(it);
auto P = s.intersection(U);
if (P and ans.count(P.value()) == 0) {
events.push(Event{P.value(), Event::INTERSECTION, s.idx});
ans.insert(P.value());
}
}
}
set<Point> bentley_ottman(vector<Segment>& segments) {
set<Point> ans;
priority_queue<Event> events;
for (size_t i = 0; i < segments.size(); ++i) {
events.push(Event{segments[i].A, Event::OPEN, i});
events.push(Event{segments[i].B, Event::CLOSE, i});
}
set<Segment> sl;
while (not events.empty()) {
auto e = events.top();
events.pop();
Segment::sweep_x = e.P.x;
switch (e.type) {
case Event::OPEN: {
auto s = segments[e.i];
sl.insert(s);
add_neighbor_intersections(s, sl, ans, events);
} break;
case Event::CLOSE: {
auto s = segments[e.i];
auto it = sl.find(s); // TODO: aqui também
if (it != sl.begin() and it != sl.end()) {
auto L = *prev(it);
auto U = *next(it);
auto P = L.intersection(U);
if (P and ans.count(P.value()) == 0)
events.push(Event{P.value(), Event::INTERSECTION, L.idx});
}
sl.erase(it);
} break;
default:
auto r = segments[e.i];
auto p = sl.equal_range(r);
vector<Segment> range(p.first, p.second);
// Remove os segmentos que se interceptam
sl.erase(p.first, p.second);
// Reinsere os segmentos
Segment::sweep_x += 0.1;
sl.insert(range.begin(), range.end());
// Procura interseções com os novos vizinhos
for (const auto& s : range)
add_neighbor_intersections(s, sl, ans, events);
}
}
return ans;
}