一、通过数组构建凸包
#include <iostream>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/convex_hull_2.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_2 Point_2;
int main()
{
// 这里定义了一个包含 5 个二维点的数组 points
Point_2 points[5] = { Point_2(0,0), Point_2(10,0),
Point_2(10,10), Point_2(6,5), Point_2(4,1) };
Point_2 result[5]; // 定义存储凸包结果的数组
Point_2* ptr = CGAL::convex_hull_2(points, points + 5, result); // 调用凸包函数
std::cout << ptr - result << " points on the convex hull:" << std::endl; // 输出凸包上点的个数
for (int i = 0; i < ptr - result; i++) { // 输出凸包上的点
std::cout << result[i] << std::endl;
}
return 0;
}
输出结果:3个点在凸包上,那另外两个点就是在凸包内部
二、通过vector构建凸包
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/convex_hull_2.h>
#include <vector>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_2 Point_2;
typedef std::vector<Point_2> Points;
int main()
{
Points points, result;
points.push_back(Point_2(0, 0));
points.push_back(Point_2(10, 0));
points.push_back(Point_2(10, 10));
points.push_back(Point_2(6, 5));
points.push_back(Point_2(4, 1));
CGAL::convex_hull_2(points.begin(), points.end(), std::back_inserter(result));
std::cout << result.size() << " points on the convex hull" << std::endl;
return 0;
}