本文来自:http://alienryderflex.com/smallest_enclosing_polygon/

这个C代码例子需要一群2维点集,如下图所示:

要获得包含这些点的最小多边形如下图所示:

查找点集最小多边形的一种方法是——将所有点都传到函数中计算。

这段代码没有充分的测试过,所以如果你有任何问题,请告诉我。这个函数可以应对重叠点的问题,如果角点上有重叠点,它只会返回一个点。

//  public-domain code by Darel Rex Finley, January 2009

#define  CIRCLE_RADIANS  6.283185307179586476925286766559

//  Determines the radian angle of the specified point (as it relates to the origin).
//
// Warning: Do not pass zero in both parameters, as this will cause division-by-zero. double angleOf(double x, double y) { double dist=sqrt(x*x+y*y) ; if (y>=0.) return acos( x/dist) ;
else return acos(-x/dist)+.5*CIRCLE_RADIANS; } // Pass in a set of 2D points in x,y,points. Returns a polygon in polyX,polyY,polyCorners.
//
// To be safe, polyX and polyY should have enough space to store all the points passed in x,y,points. void findSmallestPolygon(double *x, double *y, long points, double *polyX, double *polyY, long *polyCorners) { double newX=x[0], newY=y[0], xDif, yDif, oldAngle=.5*CIRCLE_RADIANS, newAngle, angleDif, minAngleDif ;
long i ; // Find a starting point.
for (i=0; i<points; i++) if (y[i]>newY || y[i]==newY && x[i]<newX) {
newX=x[i]; newY=y[i]; }
*polyCorners=0; // Polygon-construction loop.
while (!(*polyCorners) || newX!=polyX[0] || newY!=polyY[0]) {
polyX[*polyCorners]=newX;
polyY[*polyCorners]=newY; minAngleDif=CIRCLE_RADIANS;
for (i=0; i<points; i++) {
xDif=x[i]-polyX[*polyCorners];
yDif=y[i]-polyY[*polyCorners];
if (xDif || yDif) {
newAngle=angleOf(xDif,yDif); angleDif =oldAngle-newAngle;
while (angleDif< 0. ) angleDif+=CIRCLE_RADIANS;
while (angleDif>=CIRCLE_RADIANS) angleDif-=CIRCLE_RADIANS;
if (angleDif<minAngleDif) {
minAngleDif=angleDif; newX=x[i]; newY=y[i]; }}}
(*polyCorners)++; oldAngle+=.5*CIRCLE_RADIANS-minAngleDif; }}

最小包围多边形(凸包;最小包围点集)——C代码例子的更多相关文章

  1. opencv 6 图像轮廓与图像分割修复 2 使用多边形将轮廓包围

    使用多边形将轮廓包围 返回外部矩阵边界(boundingRect()函数) 寻找最小包围矩形(minAreaRect()函数) 寻找最小包围圆形(minEnclosingCircle函数) 用椭圆拟合 ...

  2. LightOJ 1203 Guarding Bananas (凸包最小顶角)

    题目链接:LightOJ 1203 Problem Description Once there was a lazy monkey in a forest. But he loved banana ...

  3. OpenCV 学习笔记03 边界框、最小矩形区域和最小闭圆的轮廓

    本节代码使用的opencv-python 4.0.1,numpy 1.15.4 + mkl 使用图片为 Mjolnir_Round_Car_Magnet_300x300.jpg 代码如下: impor ...

  4. bzoj 2229 [Zjoi2011]最小割(分治+最小割)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2229 [题意] 回答若干个关于割不超过x的点对数目的询问. [思路] [最小割最多有n ...

  5. 【bzoj2229】[Zjoi2011]最小割 分治+网络流最小割

    题目描述 小白在图论课上学到了一个新的概念——最小割,下课后小白在笔记本上写下了如下这段话: “对于一个图,某个对图中结点的划分将图中所有结点分成两个部分,如果结点s,t不在同一个部分中,则称这个划分 ...

  6. Opencv绘制最小外接矩形、最小外接圆

    Opencv中求点集的最小外结矩使用方法minAreaRect,求点集的最小外接圆使用方法minEnclosingCircle. minAreaRect方法原型: RotatedRect minAre ...

  7. 树的问题小结(最小生成树、次小生成树、最小树形图、LCA、最小支配集、最小点覆盖、最大独立集)

    树的定义:连通无回路的无向图是一棵树. 有关树的问题: 1.最小生成树. 2.次小生成树. 3.有向图的最小树形图. 4.LCA(树上两点的最近公共祖先). 5.树的最小支配集.最小点覆盖.最大独立集 ...

  8. Destroying The Graph 最小点权集--最小割--最大流

    Destroying The Graph 构图思路: 1.将所有顶点v拆成两个点, v1,v2 2.源点S与v1连边,容量为 W- 3.v2与汇点连边,容量为 W+ 4.对图中原边( a, b ), ...

  9. 【poj3522-苗条树】最大边与最小边差值最小的生成树,并查集

    题意:求最大边与最小边差值最小的生成树.n<=100,m<=n*(n-1)/2,没有重边和自环. 题解: m^2的做法就不说了. 时间复杂度O(n*m)的做法: 按边排序,枚举当前最大的边 ...

随机推荐

  1. WebStorm中Node.js项目配置教程(1)——创建项目

    Node.js绝对是一个web开发的热点话题,作为web神器的WebStorm也是开发Node.js的佼佼者. 接下来就Node.js项目在WebStorm的配置操作就行详细的讲解,首先是创建项目.两 ...

  2. PHP 调用asp.net Web Services服务问题总结

    原文:PHP 调用asp.net Web Services服务问题总结 PHP是弱类型语言,转换非常不方便. < ?php //soap 客户端 $client=new SoapClient(' ...

  3. leetcode第19题--Remove Nth Node From End of List

    Problem: Given a linked list, remove the nth node from the end of list and return its head. For exam ...

  4. C#网络编程系列(两)它Socket同步TCPserver

    声明原文 笔者:竹zz  本文地址http://blog.csdn.net/zhujunxxxxx/article/details/44258719 转载请注明出处 文章系列文件夹 C#网络编程系列文 ...

  5. unbuntu下的root 用户和 sudo 命令

    参考: http://james23dier.iteye.com/blog/721246 http://blog.csdn.net/shichexixi/article/details/5969993 ...

  6. C# 通讯网关开发

    C# 通讯网关开发 楼主从12年毕业大部分时间一直从事于通讯网关的开发,刚刚学那会连C#是啥都不知道,就直接入手网关开发,前前后后到现在也算是弄了5.6个通讯协议,后来看到北风之神的socket框架和 ...

  7. sqlclr返回数据集案例

    ----------------------------------------------返回一张表,但只有一条数据,最后一次设置的. [Microsoft.SqlServer.Server.Sql ...

  8. web中纯java获取配置文件中的数据

    /*********获取配置文件,但配置文件中的值改变,不会随着值的改变也获取的参数值改变**********/  /**   * 原因是因为,类装载,装载完后,不会再去装载了   * *///  I ...

  9. Julia is a high-level, high-performance dynamic programming language for technical computing, with syntax that is familiar to users of other technical

    http://julialang.org/ julia | source | downloads | docs | blog | community | teaching | publications ...

  10. IOS UI 第八篇:基本UI

    实现图片的滚动,并且自动停止在每张图片上     - (void)viewDidLoad{    [super viewDidLoad]; UIScrollView *scrollView = [[U ...