二维凸包构建

一、通过数组构建凸包

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值