gpt4 book ai didi

c++ - 用cgal计算两个多边形的交集面积

转载 作者:太空狗 更新时间:2023-10-29 21:26:49 30 4
gpt4 key购买 nike

给定两个凸多边形的顶点,使用 cgal 计算它们相交面积的最简单方法是什么?

最佳答案

因为您使用的是凸多边形,所以无需担心孔洞。所以我能想到的最简单的代码基本上是构造多边形、调用交集、循环交集并计算面积总和::

#include <iostream>
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/Polygon_with_holes_2.h>
#include <CGAL/Boolean_set_operations_2.h>
#include <CGAL/Polygon_2_algorithms.h>


typedef CGAL::Simple_cartesian<double> K;
typedef K::Point_2 Point;
typedef CGAL::Polygon_2<K> Polygon_2;
typedef CGAL::Polygon_with_holes_2<K> Polygon_with_holes_2;
using std::cout; using std::endl;


int main(){
Point points[] = { Point(0,0), Point(1,0), Point(1,1), Point(0,1)};
Point points2[] = { Point(0.5,0.5), Point(1.5,0.5), Point(1.5,1.5), Point(0.5,1.5)};
Polygon_2 poly1(points, points+4);
Polygon_2 poly2(points2, points2+4);
//CGAL::General_polygon_with_holes_2<K> poly3;
std::list<Polygon_with_holes_2> polyI;

CGAL::intersection(poly1, poly2, std::back_inserter(polyI));

double totalArea = 0;
typedef std::list<Polygon_with_holes_2>::iterator LIT;
for(LIT lit = polyI.begin(); lit!=polyI.end(); lit++){
totalArea+=lit->outer_boundary().area();
}
cout << "TotalArea::" << totalArea;

}

关于c++ - 用cgal计算两个多边形的交集面积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10502672/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com