CGAL 点集的凸包(一)

点集的凸包(一)

Code

利用数组进行输入输出

#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()
{
  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];
  // ptr - result : 地址偏移量即点的个数 ptr:指针移动的下一个位置
  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;
}

标准库中的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;
}

Result

3 points on the convex hull:
0 0
10 0
10 10

vector示例

3 points on the convex hull

Expalnation

准确断言和非准确构建的kernel来生成程序。

链接

点集的凸包(一)

Reference

  1. Hello World 之 CGAL
  2. tutorial_hello_world

插曲

back_inserter用于在末尾插入元素。
实现方法是构造一个迭代器,这个迭代器可以在容器末尾添加元素。
这个迭代器是以插入(insert)方式而非覆盖(overwrite)方式运作的。

可以使用back_inserter的容器是有push_back成员函数的容器,比如vector, dequelist等。

在末尾添加一个元素
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>


int main()
{
    std::vector<int> v1{ 1, 2, 3, 4, 5, 6 };

    *(std::back_inserter(v1)) = 10;

    std::cout << "v1:   ";
    for (int i : v1) {
        std::cout << i << "\t";
    }
    std::cout << std::endl;

    return 0;
}

结果:

v1:   1    2       3       4       5       6       10
拷贝v2到v1的末尾
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

int main()
{
    std::vector<int> v1{ 1, 2, 3, 4, 5, 6};
    std::vector<int> v2{ 10, 20, 30, 40, 50, 60};

    std::copy(v2.begin(), v2.end(), std::back_inserter(v1));

    std::cout << "v1:   ";
    for (int i : v1) {
        std::cout << i << "\t";
    }
    std::cout << std::endl;

    return 0;
}

结果:

v1:   1 2       3       4       5       6       10      20      30      40      50      60
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值