本文来自: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; }}

最新文章

  1. SuperMap iServer 扩展服务及扩展服务提供者范例
  2. 初学Python之os模块
  3. angular.js学习笔记之一
  4. codeforces 478B Random Teams 解题报告
  5. SharePoint表单和工作流 - Nintex篇(七)
  6. 正则表达式2&mdash;&mdash;grep命令
  7. RAR压缩解压命令
  8. java之装饰器模式
  9. Java与算法之(9) - 直接插入排序
  10. 从一个简洁的进度刻度绘制中了解自定义View的思路流程
  11. java事件监听机制
  12. 浅析MySQL 8忘记密码处理方式
  13. hightopo学习之旅一 -- 节点动画
  14. Python——爬虫进阶
  15. JavaScript基础理解及技巧(入门)
  16. Java之旅_面向对象_重写和重载
  17. Centos ssh 限制ip访问
  18. 谷歌商店高级搜索 Google play advanced search
  19. Android activity之间数据传递和共享的方式之Application
  20. python函数总结

热门文章

  1. POJ1320 Street Numbers【佩尔方程】
  2. WeChatAPI 开源系统架构详解
  3. 打印Ibatis最后,SQL声明
  4. CF - 96D - Volleyball
  5. 实例学习SSIS(五)--理论介绍SSIS
  6. C#泛型回顾点滴
  7. 【转】在PC上测试移动端网站和模拟手机浏览器的5大方法
  8. QTP使用技巧
  9. Web API实现POST报文的构造与推送
  10. 在线压缩JS的工具