void Polygon clip const Line line std vector Line result const std vec

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
void Polygon::clip(const Line &line, std::vector<Line> &result) const {
std::vector<double> inters;
for (int i = 0; i < bound.size() - 1; i++) {
double t = line.intersect({bound[i], bound[i + 1]});
if (t != -1) {
inters.push_back(t);
}
}
if (inters.size() % 2 == 1) {
inters.push_back(contains(line.a) ? 0 : 1);
} else if (contains(line.a) && contains(line.b)) {
inters.push_back(0);
inters.push_back(1);
}
std::sort(inters.rbegin(), inters.rend());
for (int i = 0; i < inters.size(); i += 2) {
result.push_back({line.getPoint(inters[i]), line.getPoint(inters[i + 1])});
}
}