有没有求矩形点云凸包,并求出矩形点云四个角点坐标的c++代码呢
时间: 2024-02-02 10:03:55 浏览: 120
是的,以下是求解矩形点云凸包并求出矩形点云四个角点坐标的C++代码示例:
```c++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN = 10005;
const double EPS = 1e-8;
struct Point {
double x, y;
Point() {}
Point(double x, double y) : x(x), y(y) {}
friend Point operator+(const Point& a, const Point& b) {
return Point(a.x + b.x, a.y + b.y);
}
friend Point operator-(const Point& a, const Point& b) {
return Point(a.x - b.x, a.y - b.y);
}
friend double operator*(const Point& a, const Point& b) {
return a.x * b.y - a.y * b.x;
}
};
struct Line {
Point p, v;
double ang;
Line() {}
Line(Point p, Point v) : p(p), v(v) {
ang = atan2(v.y, v.x);
}
friend bool operator<(const Line& l1, const Line& l2) {
return l1.ang < l2.ang;
}
};
Point p[MAXN], s[MAXN];
int n, m, top;
bool cmp(Point a, Point b) {
if (a.y != b.y) {
return a.y < b.y;
}
return a.x < b.x;
}
bool gtr(double a, double b) {
return a - b > EPS;
}
Point get_line_intersection(Line l1, Line l2) {
Point p1 = l1.p, v1 = l1.v, p2 = l2.p, v2 = l2.v;
Point u = p1 - p2;
double t = (v2 * u) / (v1 * v2);
return p1 + v1 * t;
}
void HalfPlaneIntersection() {
sort(p + 1, p + n + 1, cmp);
m = 0;
for (int i = 1; i <= n; i++) {
while (m >= 2 && gtr((s[m] - s[m - 1]) * (p[i] - s[m]), 0)) {
m--;
}
s[++m] = p[i];
}
top = 1;
vector<Line> q(m + 1);
for (int i = 1; i <= m; i++) {
q[i] = Line(s[i], s[i + 1] - s[i]);
}
sort(q.begin() + 1, q.end());
for (int i = 2; i <= m; i++) {
if (gtr(q[i].ang, q[top].ang) + gtr(q[i].ang, q[top + 1].ang) == 1) {
break;
}
top++;
}
Line l1 = Line(s[top], s[top + 1] - s[top]);
Line l2 = Line(s[top % m + 1], s[top + 1] - s[top % m + 1]);
Point p1 = get_line_intersection(l1, l2);
Line l3 = Line(s[top % m + 1], s[(top + 2) % m + 1] - s[top % m + 1]);
Point p2 = get_line_intersection(l1, l3);
Line l4 = Line(s[top + 1], s[(top + 2) % m + 1] - s[top + 1]);
Point p3 = get_line_intersection(l4, l3);
Line l5 = Line(s[top], s[(top + 2) % m + 1] - s[top]);
Point p4 = get_line_intersection(l4, l5);
cout << p1.x << " " << p1.y << endl;
cout << p2.x << " " << p2.y << endl;
cout << p3.x << " " << p3.y << endl;
cout << p4.x << " " << p4.y << endl;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> p[i].x >> p[i].y;
}
HalfPlaneIntersection();
return 0;
}
```
其中,输入格式为:
```
n
x1 y1
x2 y2
...
xn yn
```
其中,n为矩形点云的点数,接下来n行为矩形点云的点的坐标。输出格式为四个角点的坐标。
阅读全文