代码记录-CGAL算法库-求线相交与面相交

添加头文件

#include 
#include 

添加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                           Polygon_2;
typedef CGAL::Polygon_with_holes_2                Polygon_with_holes_2;
typedef std::list                   Pwh_list_2;
typedef CGAL::Lazy_exact_nt                   doubleCGAL;
typedef boost::optional,CGAL::Line_2 > >   PointResult;

线相交:

		//线线相交
		Segment_2 seg(Point_2(1, 0), Point_2(1, 2));//线段
		Line_2 lin(1, -1, 0);//直线
		CGAL::cpp11::result_of::type
			result = intersection(seg, lin);//求交
		if (result)
		{
			if (const Segment_2* s = boost::get(&*result))
			{
				//           相交结果是线段S;
			}
			else
			{
				const Point_2* p = boost::get(&*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();

 

你可能感兴趣的:(C++)