多边形裁剪的 Sutherland—Hodgman 算法的c++代码实现
时间: 2024-03-01 18:54:57 浏览: 78
多边形裁剪的Sutherland—Hodgman算法
4星 · 用户满意度95%
以下是Sutherland-Hodgman算法的C++代码实现,假设多边形为poly,裁剪窗口为clip:
```c++
vector<Point> Sutherland_Hodgman(vector<Point> poly, vector<Point> clip) {
int n = clip.size();
for (int i = 0; i < n; i++) {
int m = poly.size();
vector<Point> newpoly;
Point S = poly[m-1];
for (int j = 0; j < m; j++) {
Point E = poly[j];
if (inside(E, clip[i])) {
if (!inside(S, clip[i])) {
newpoly.push_back(intersect(S, E, clip[i]));
}
newpoly.push_back(E);
} else if (inside(S, clip[i])) {
newpoly.push_back(intersect(S, E, clip[i]));
}
S = E;
}
poly = newpoly;
}
return poly;
}
```
其中,inside函数用于判断点是否在裁剪窗口内,intersect函数用于求解直线与边界的交点。
阅读全文