添加头文件
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Boolean_set_operations_2.h>
添加typedef
typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
typedef Kernel::Point_2 Point_2;
typedef Kernel::Segment_2 Segment_2;
typedef Kernel::Line_2 Line_2;
typedef Kernel::Vector_2 Vector_2;
typedef Kernel::Direction_2 Direction_2;
typedef Kernel::Ray_2 Ray_2;
typedef Kernel::Intersect_2 Intersect_2;
typedef CGAL::Polygon_2<Kernel> Polygon_2;
typedef CGAL::Polygon_with_holes_2<Kernel> Polygon_with_holes_2;
typedef std::list<Polygon_with_holes_2> Pwh_list_2;
typedef CGAL::Lazy_exact_nt<CGAL::Gmpq> doubleCGAL;
typedef boost::optional<boost::variant<CGAL::Point_2<CGAL::Epeck>,CGAL::Line_2<CGAL::Epeck> > > PointResult;
线相交:
//线线相交
Segment_2 seg(Point_2(1, 0), Point_2(1, 2));//线段
Line_2 lin(1, -1, 0);//直线
CGAL::cpp11::result_of<Intersect_2(Segment_2, Line_2)>::type
result = intersection(seg, lin);//求交
if (result)
{
if (const Segment_2* s = boost::get<Segment_2>(&*result))
{
// 相交结果是线段S;
}
else
{
const Point_2* p = boost::get<Point_2 >(&*result);
// 相交结果是点p;
}
}
面相交:
Polygon_2 P11;
P11.push_back(Point_2(0, 0));
P11.push_back(Point_2(4, 0));
P11.push_back(Point_2(2, 2));
Polygon_2 Q11;
Q11.push_back(Point_2(0, 2));
Q11.push_back(Point_2(2, 0));
Q11.push_back(Point_2(4, 2));
Polygon_2 tempQ = Q11; std::cout << "Q = "; print_polygon(tempQ);
Polygon_2 tempP = P11; std::cout << "P = "; print_polygon(tempP);
if (CGAL::do_intersect(tempQ, tempP))//判断,不输出结果
{
std::cout << "相交" << std::endl;
}
else
{
std::cout << "不相交" << std::endl;
}
//求结果
Pwh_list_2 intR;
Pwh_list_2::const_iterator it;
CGAL::intersection(tempP, tempQ, std::back_inserter(intR));
for (it = intR.begin(); it != intR.end(); ++it)
{
Polygon_2 pt = it->outer_boundary();//返回polygon
}
其他:
//求面积(顺时针为负)
if (poly.area() < 0)
{
poly.reverse_orientation();//顺序倒转
}
//数据类型转为double
//坐标值,面积什么的,都是CGAL定义的类似double的数据
polygon.area().exact().to_double();