计算多边形重点面积
Polygon.h
#pragma once
#include "Point.h"
#include <vector>
using namespace std;
class Polygon {
public:
Polygon(float* polygon, int vertex) : polygon(polygon), vertex(vertex) {};
int getVertex() const { return vertex; }
float* getPolygon() const { return polygon; }
private:
float* polygon;
const int vertex;
};
Geometry.h
#pragma once
#include "Polygon.h"
#include "Point.h"
#include <vector>
int dcmp(float x);
float Cross(const Point& a,const Point& b,const Point& c);
Point IntersectionPoint(const Point& a,const Point& b,const Point& c,const Point& d);
float PolygonArea(const std::vector<Point>& p, int n);
float ConvexIntersectArea(const std::vector<Point>& a,const std::vector<Point>& b, int na, int nb);
float SimpleIntersectArea(const std::vector<Point>& a,const std::vector<Point>& b, int na, int nb);
float Intersection(const Polygon& A, const Polygon& B);
std::vector<Point> PointToArrayPoint(float* polygon, int vertex);
Point.h
#pragma once
struct Point
{
float x,y;
};
Polygon.cpp
#include "Polygon.h"
#include "Geometry.h"
#include <cmath>
const int maxn = 300;
const float eps = (float)1e-6;
int dcmp(float x)
{
if (x > eps) {
return 1;
}
return x < -eps ? -1 : 0;
}
float Cross(const Point& a,const Point& b,const Point& c)
{
return (a.x - c.x) * (b.y - c.y) - (b.x - c.x) * (a.y - c.y);
}
Point IntersectionPoint(const Point& a,const Point& b,const Point& c,const Point& d)
{
float numerator = (a.x - c.x) * (c.y - d.y) - (a.y - c.y) * (c.x - d.x);
float denominator = (a.x - b.x) * (c.y - d.y) - (a.y - b.y) * (c.x - d.x);
float t = numerator / denominator;
Point p(a);
p.x += (b.x - a.x) * t;
p.y += (b.y - a.y) * t;
return p;
}
float PolygonArea(const std::vector<Point>& p, int n)
{
if (n < 3) {
return 0.0;
}
float s = p[0].y * (p[n - 1].x - p[1].x);
for (int i = 1; i < n - 1; ++i) {
s += p[i].y * (p[i - 1].x - p[i + 1].x);
}
s += p[n - 1].y * (p[n - 2].x - p[0].x);
return fabsf(s * 0.5);
}
float ConvexIntersectArea(const std::vector<Point>& a,const std::vector<Point>& b, int na, int nb)
{
std::vector<Point> p, tmp;
p.resize(20);
tmp.resize(20);
int tn = 0;
int startFlag = 0;
int endFlag = 0;
for (int i = 0; i < nb; i++)
{
p[i] = b[i];
}
for (int i = 0; i < na && nb > 2; i++)
{
if (i == na - 1) {
startFlag = dcmp(Cross(a[0], p[0], a[i]));
}
else {
startFlag = dcmp(Cross(a[i + 1], p[0], a[i]));
}
for (int j = tn = 0; j < nb; j++)
{
int startFlag = endFlag;
if (startFlag >= 0) {
tmp[tn] = p[j];
tn += 1;
}
if (i == na - 1) {
if (j == nb - 1) {
endFlag = dcmp(Cross(a[0], p[0], a[i]));
}
else {
endFlag = dcmp(Cross(a[0], p[j + 1], a[i]));
}
}
else {
if (j == nb - 1) {
endFlag = dcmp(Cross(a[i + 1], p[0], a[i]));
}
else {
endFlag = dcmp(Cross(a[i + 1], p[j + 1], a[i]));
}
}
if ((startFlag ^ endFlag) == -2) {
if (i == na - 1) {
if (j == nb - 1) {
tmp[tn] = IntersectionPoint(a[i], a[0], p[j], p[0]);
tn += 1;
}
else {
tmp[tn] = IntersectionPoint(a[i], a[0], p[j], p[j + 1]);
tn += 1;
}
}
else {
if (j == nb - 1) {
tmp[tn] = IntersectionPoint(a[i], a[i + 1], p[j], p[0]);
tn += 1;
}
else {
tmp[tn] = IntersectionPoint(a[i], a[i + 1], p[j], p[j + 1]);
tn += 1;
}
}
}
}
for (int j = 0; j < tn; j++)
{
p[j] = tmp[j];
}
nb = tn;
p[nb] = p[0];
}
if (nb < 3) {
return 0.0;
}
return PolygonArea(p, nb);
}
float SimpleIntersectArea(const std::vector<Point>& a,const std::vector<Point>& b, int na, int nb)
{
int i = 0;
int j = 0;
std::vector<Point> t1, t2;
t1.resize(na);
t2.resize(nb);
float res = 0;
float num1, num2;
t1[0] = a[0];
t2[0] = b[0];
for (i = 2; i < na; i++)
{
t1[1] = a[i - 1];
t1[2] = a[i];
num1 = dcmp(Cross(t1[1], t1[2], t1[0]));
if (num1 < 0) {
std::swap(t1[1], t1[2]);
}
for (j = 2; j < nb; j++)
{
t2[1] = b[j - 1];
t2[2] = b[j];
num2 = dcmp(Cross(t2[1], t2[2], t2[0]));
if (num2 < 0) {
std::swap(t2[1], t2[2]);
}
res += ConvexIntersectArea(t1, t2, 3, 3) * num1 * num2;
}
}
return res;
}
float Intersection(const Polygon& A, const Polygon& B)
{
std::vector<Point> a = PointToArrayPoint(A.getPolygon(), A.getVertex());
std::vector<Point> b = PointToArrayPoint(B.getPolygon(), B.getVertex());
return SimpleIntersectArea(a, b, (int)a.size(), (int)b.size());
}
std::vector<Point> PointToArrayPoint(float* polygon, int vertex)
{
std::vector<Point> RetPolygonPoint;
RetPolygonPoint.resize(vertex);
for (int i = 0; i < vertex; i++)
{
RetPolygonPoint[i].x = polygon[i * 2];
RetPolygonPoint[i].y = polygon[i * 2 + 1];
}
return RetPolygonPoint;
}
main.cpp
#include "Polygon.h"
#include "Geometry.h"
#include <iostream>
int main()
{
float coordsA[8]{ 10, 20, 10, 30, 30, 50, 40, 20};
float coordsB[8]{ 15, 25, 15, 35, 35, 55, 45, 25 };
Polygon a(coordsA, 4);
Polygon b(coordsB, 4);
std::cout << "两个多边形的重叠面积为:## " << Intersection(a, b) << endl;
return 0;
}