|
#include <iostream>
#include <vector>
using namespace std;
// 定义点的结构体
struct Point {
int x, y;
Point(int x = 0, int y = 0) : x(x), y(y) {}
};
// 计算两点之间的距离
double distance(Point p1, Point p2) {
return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}
// 判断点是否在多边形内部
bool isInside(Point p, vector<Point>& polygon) {
int n = polygon.size();
bool flag = false;
double d;
for (int i = 0, j = n - 1; i < n; j = i, i++) {
d = (polygon[i].y - p.y) * (polygon[j].x - polygon[i].x) -
(polygon[i].x - p.x) * (polygon[j].y - polygon[i].y);
if (d == 0) {
return false; // 点在边上
}
else if (d > 0) {
flag = !flag; // 穿过了多边形的一条边
}
}
return flag;
}
// 计算多边形的面积
double polygonArea(vector<Point>& polygon) {
int n = polygon.size();
double area = 0.0;
for (int i = 0, j = n - 1; i < n; j = i, i++) {
area += (polygon[i].x + polygon[j].x) * (polygon[i].y - polygon[j].y);
}
area = abs(area) / 2.0;
return area;
}
// 临界多边形算法
vector<Point> criticalPolygon(vector<Point>& polygon) {
vector<Point> criticalPolygon;
int n = polygon.size();
for (int i = 0; i < n; i++) {
// 判断点是否在多边形内部
if (isInside(polygon[i], polygon)) {
criticalPolygon.push采用back(polygon[i]);
}
}
return criticalPolygon;
}
int main() {
vector<Point> polygon = { Point(0, 0), Point(0, 5), Point(5, 5), Point(5, 0) };
vector<Point> criticalPolygon = criticalPolygon(polygon);
cout << "The vertices of the critical polygon are:" << endl;
for (int i = 0; i < criticalPolygon.size(); i++) {
cout << "(" << criticalPolygon[i].x << ", " << criticalPolygon[i].y << ")" << endl;
}
double area = polygonArea(criticalPolygon);
cout << "The area of the critical polygon is: " << area << endl;
return 0;
} |
|