计算两个不规则多边形重叠的面积

10 篇文章 0 订阅
#include <iostream>
#include <cmath>
#include <cstring>

using namespace std;

const int maxn = 300;
const double eps = 1e-6;
int dcmp(double x)
{
    if(x > eps) return 1;
    return x < -eps ? -1 : 0;
}
struct Point
{
    double x, y;
};
double cross(Point a,Point b,Point c) ///叉积
{
    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
Point intersection(Point a,Point b,Point c,Point d)
{
    Point p = a;
    double t =((a.x-c.x)*(c.y-d.y)-(a.y-c.y)*(c.x-d.x))/((a.x-b.x)*(c.y-d.y)-(a.y-b.y)*(c.x-d.x));
    p.x +=(b.x-a.x)*t;
    p.y +=(b.y-a.y)*t;
    cout << "intersection p.x=" << p.x << ", p.y=" << p.y << endl;
    return p;
}
//计算多边形面积
double PolygonArea(Point p[], int n)
{
    if(n < 3) return 0.0;
    double 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);
        // cout << "p[i-1].x =" << p[i-1].x << ", p[i-1].y=" << p[i-1].y << endl;
        // cout << "p[i].x =" << p[i].x << ", p[i].y=" << p[i].y << endl;
        // cout << "p[i+1].x =" << p[i+1].x << ", p[i+1].y=" << p[i+1].y << endl;
    }
    s += p[n - 1].y * (p[n - 2].x - p[0].x);
    cout << "s =" << s << endl;
    return fabs(s * 0.5);
}
double CPIA(Point a[], Point b[], int na, int nb)//ConvexPolygonIntersectArea
{
    Point p[20], tmp[20];
    int tn, sflag, eflag;
    memcpy(p,b,sizeof(Point)*(nb));
    for(int i = 0; i < na && nb > 2; i++)
    {
    	if (i == na - 1) {
    		sflag = dcmp(cross(a[0], p[0],a[i]));
    	} else {
    		sflag = dcmp(cross(a[i + 1], p[0],a[i]));
    	}
        for(int j = tn = 0; j < nb; j++, sflag = eflag)
        {
            if(sflag>=0) {
            	tmp[tn++] = p[j];
            }
            if (i == na - 1) {
            	if (j == nb -1) {
            		eflag = dcmp(cross(a[0], p[0], a[i]));
				} else {
					eflag = dcmp(cross(a[0], p[j + 1], a[i]));
				}
			} else {
				if (j == nb -1) {
					eflag = dcmp(cross(a[i + 1], p[0], a[i]));
				} else {
					eflag = dcmp(cross(a[i + 1], p[j + 1], a[i]));
				}
			}
            if((sflag ^ eflag) == -2){
            	if (i == na - 1) {
            		if (j == nb -1) {
            			tmp[tn++] = intersection(a[i], a[0], p[j], p[0]); //求交点
            		} else {
            			tmp[tn++] = intersection(a[i], a[0], p[j], p[j + 1]);
            		}
				} else {
					if (j == nb -1) {
						tmp[tn++] = intersection(a[i], a[i + 1], p[j], p[0]);
					} else {
						tmp[tn++] = intersection(a[i], a[i + 1], p[j], p[j + 1]);
					}
				}
            }
        }
        memcpy(p, tmp, sizeof(Point) * tn);
        nb = tn, p[nb] = p[0];
    }
    if(nb < 3) return 0.0;
    return PolygonArea(p, nb);
}
double SPIA(Point a[], Point b[], int na, int nb)///SimplePolygonIntersectArea 调用此函数
{
    int i, j;
    Point t1[na], t2[nb];
    double res = 0, 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) 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) swap(t2[1], t2[2]);
            res += CPIA(t1, t2, 3, 3) * num1 * num2;
        }
    }
    cout << "Sum::res=" <<res << endl;
    return res;
}
Point p1[maxn], p2[maxn];
int n1, n2;
int main()
{

    while(cin>>n1>>n2)
    {
        for(int i = 0; i < n1; i++) scanf("%lf%lf", &p1[i].x, &p1[i].y);
        for(int i = 0; i < n2; i++) scanf("%lf%lf", &p2[i].x, &p2[i].y);
        double Area = SPIA(p1, p2, n1, n2);
        cout << "Area=" << Area << endl;
        double A1 = PolygonArea(p1, n1);
        double A2 = PolygonArea(p2, n2);
        cout << "A1 =" << A1 << ", A2=" << A2 << endl;
    }
    return 0;
}

测试结果1:

输入的参数为:
4 4
0 0 0 3 5 3 5 0
3 1 3 4 6 4 6 1

产生的过程的log和结果:
intersection p.x=5, p.y=3
intersection p.x=3, p.y=1.8
intersection p.x=3, p.y=3
s =2.4
intersection p.x=6, p.y=3.6
intersection p.x=5, p.y=3
intersection p.x=5, p.y=3
intersection p.x=5, p.y=4
intersection p.x=3, p.y=1.8
s =1.6
intersection p.x=5, p.y=1
intersection p.x=5, p.y=3
s =4
Sum::res=4
Area=4
s =-30
s =-18
A1 =15, A2=9

测试结果2:

输入的参数为:
4 5
3 1 6 1 6 4 3 4
3 0 5 1 5 3 3 1 0 2

产生的过程的log和结果:
intersection p.x=3.66667, p.y=1
s =2.66667
intersection p.x=3.66667, p.y=1
s =1.33333
intersection p.x=1.5, p.y=1
intersection p.x=2.4, p.y=0.4
Sum::res=2
Area=2
s =18
s =9
A1 =9, A2=4.5
  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值