用cgal计算两个多边形的相交面积 [英] Compute intersection area of two polygons with cgal

查看:715
本文介绍了用cgal计算两个多边形的相交面积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Given the vertices of two convex polygons, what is the simplest way of computing the area of their intersection using cgal?

推荐答案

由于使用凸多边形,因此无需担心孔。因此,我能想到的最简单的代码基本上是构造多边形,调用交集,在交集上循环并总计面积:

Because you are working with convex polygons, there is no need to worry about holes. So the simplest code that I can think of is basically construct the polygons, call intersection, loop over intersection and total up the area::

#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;

}

这篇关于用cgal计算两个多边形的相交面积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆