算法原理CSDN或者知乎上都有,不难。

代码主要参考C语言实现凸包Graham_scan算法

但是一只小蒟蒻的代码没有经过测试,细节上有一些小问题。我将代码修改测试后,放下下面,防走失。

将vector结构去除,就可以改为C语言代码。

#include<math.h>
#include<vector>#define PI 3.1415926typedef struct Point {int x, y;double angle;
}Point;int less(Point a, Point b) {     //定义点a小于点b法则(排序用)if (a.angle != b.angle) return a.angle < b.angle;else return a.x < b.x;
}int ifAnticlockwise(Point a, Point b, Point c) {   //是否为逆时针int crossProduct = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);  //求向量积return crossProduct > 0;   //向量积为正表示逆时针
}std::vector<Point> Graham_scan(std::vector<Point> point) {double sx = HUGE_VAL, sy = HUGE_VAL;for (int i = 0; i < point.size(); i++) {     //寻找起始点if (point[i].y < sy)sx = point[i].x, sy = point[i].y;else if (point[i].y == sy && point[i].x < sx)sx = point[i].x;}for (int i = 0; i < point.size(); i++) {     //求夹角if (point[i].x == sx && point[i].y == sy)point[i].angle = 0;else if (point[i].x == sx)point[i].angle = PI / 2;else {point[i].angle = atan((point[i].y - sy) * 1.0 / (point[i].x - sx));if (point[i].angle < 0)point[i].angle = PI + point[i].angle;//转换为与x轴正方向夹角}}for (int i = 0; i < point.size() - 1; i++) {for (int j = i + 1; j < point.size(); j++) {  //冒泡排序if (less(point[j], point[i])) {Point temp = point[i];point[i] = point[j];point[j] = temp;}}}int cnt = 3;  //栈顶指针for (int i = 3; i < point.size(); ) {if (ifAnticlockwise(point[cnt - 2], point[cnt - 1], point[i]))point[cnt++] = point[i++];elsepoint[--cnt] = point[i];}std::vector<Point> convex_hull;for (int i = cnt - 1; i >= 0; i--) {    //逆序输出convex_hull.push_back(point[i]);}return convex_hull;
}

测试用main,将点与凸包均绘制在图上,结果正确与否比较直观。

#include<stdio.h>
#include"opencv2/opencv.hpp"int main() {srand((unsigned)time(NULL));int n = 19, minx = 1, maxx = 98, miny = 1, maxy = 98;std::vector<Point> point;for (int i = 0; i < n; i++) {  //生成随机坐标Point pt;pt.x = rand() % (maxx - minx + 1) + minx;pt.y = rand() % (maxy - miny + 1) + miny;point.push_back(pt);}//point[0].x = 6; point[0].y = 80;//point[1].x = 72; point[1].y = 18;//point[2].x = 62; point[2].y = 65;//point[3].x = 62; point[3].y = 12;//point[4].x = 6; point[4].y = 71;//point[5].x = 79; point[5].y = 48;//point[6].x = 91; point[6].y = 44;//point[7].x = 4; point[7].y = 65;//point[8].x = 87; point[8].y = 2;//point[9].x = 31; point[9].y = 56;//point[10].x = 79; point[10].y = 48;//point[11].x = 67; point[11].y = 78;//point[12].x = 19; point[12].y = 49;//point[13].x = 48; point[13].y = 38;//point[14].x = 25; point[14].y = 26;//point[15].x = 39; point[15].y = 50;//point[16].x = 65; point[16].y = 98;//point[17].x = 24; point[17].y = 94;//point[18].x = 78; point[18].y = 62;for (int i = 0; i <n; i++) {printf("point[%d].x = %d;point[%d].y = %d;\n", i,point[i].x, i, point[i].y);}cv::Mat showMat(100, 100, CV_8UC1, cv::Scalar(0));for (int i = 0; i < n; i++) {cv::circle(showMat, cv::Point(point[i].x, point[i].y), 2, cv::Scalar(255));}std::vector<Point> convex_hull = Graham_scan(point);for (int i = 1; i < convex_hull.size(); i++) {  //逆序输出cv::line(showMat, cv::Point(convex_hull[i].x, convex_hull[i].y), cv::Point(convex_hull[i - 1].x, convex_hull[i - 1].y), cv::Scalar(125), 1, cv::LINE_4);}cv::line(showMat, cv::Point(convex_hull[0].x, convex_hull[0].y), cv::Point(convex_hull.back().x, convex_hull.back().y), cv::Scalar(200), 1, cv::LINE_4);return 0;
}

C++实现凸包Graham_scan算法相关推荐

  1. HDUOJ 1392凸包graham算法

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  2. c++ 凸包 分治算法_三维凸包

    缘起 众所周知,二维凸包可以使用 Graham 扫描 内解决. 所以本文来学习一下三维空间中凸包的一种直观算法--增量算法(increment algorithm) 分析 有一条叫 Willy 的苹果 ...

  3. Java:实现GrahamScan凸包问题算法(附完整源码)

    Java:实现GrahamScan凸包问题算法 package com.williamfiset.algorithms.geometry;import java.awt.geom.Point2D; i ...

  4. JavaScript:实现Convex hull凸包问题算法(附完整源码)

    JavaScript:实现Convex hull凸包问题算法 function compare (a, b) {// Compare Function to Sort the points, a an ...

  5. matlab 凸包质心算法,求多边形凸包(线性算法)--陈氏凸包算法--Computing the convex hull of a simple polygon(源码)...

    陈氏凸包算法-算法参考:Computing the convex hull of a simple polygon 作者:Chern-Lin Chen 陈氏算法提供了一个线性效率求凸包的算法,本文使用 ...

  6. java经纬度凸包graham_计算几何-凸包-Graham算法

    一.点集有序化-水平排序 在计算几何中,点集往往无序,因此在计算前需要对点集进行排序,使得算法可以有序高效运行. 水平排序利用点在二维平面上固有的横纵坐标属性进行排序,只涉及点坐标的比较,与极坐标排序 ...

  7. matlab 凸包质心算法,python 生成任意形状的凸包图代码

    一.效果图: 在左图的白色区域周围,画任意形状的凸包图. 二.代码 import cv2 import numpy as np def generate_poly(image, n, area_thr ...

  8. 平面凸包(jarvis算法)

    算法大体思想:形成凸包的构造函数是这样的,找到一条直线l过其中一点(记为A)并且所有其他点都在l的同侧(显然这样的直线一定可以找到),则A必为凸包上的一点.让l以A为轴点向一个方向(比如:顺时针方向) ...

  9. 算法 - 凸包(Graham算法)

    定义:在一个实数向量空间V中,对于给定集合X,所有包含X的凸集的交集S被称为X的凸包 前置:B点位于向量  的左边,  与 叉积为正 B点位于向量  的右边, 与  叉积为负             ...

最新文章

  1. 20155222 2016-2017-2 《Java程序设计》第10周学习总结
  2. centos6.5环境 安装php5.5.30的redis扩展 介绍
  3. 佛吉尼亚大学计算机世界排名,弗吉尼亚大学计算机世界排名
  4. 一个数据仓库时代开始--Hive
  5. 感知器的c++实现_使用FastAI和PyTorch的多层感知器
  6. 【POJ - 3041】Asteroids (二分图,最小点覆盖)
  7. mysql 触发器不能同时 insert or update or delete_MySQL6:触发器
  8. mysql获取数据库名_mysql获取数据库名
  9. docker部署consol 集群
  10. 1人民币试用世纪互联azure虚拟机,跑CNN训练
  11. python的复数的实部虚部都是浮点数吗_python中复数的共轭复数知识点总结
  12. 南邮 OJ 1128 An Industrial Spy
  13. 数学--计算几何--三角定位原理和升级
  14. python 导入sklearn时报错: no model named ‘murmurhash‘
  15. 【001】光学系统的像质评价方法
  16. 线性代数第一遍思维导图
  17. UE4实现生化危机7影子谜题
  18. 了解RAC(ReactiveCocoa)
  19. Dell XPS15 4K GTX1050 ubuntu16.04系统安装+分辨率设置
  20. 几百套AE模板企业片头动画

热门文章

  1. 前端从一只小白到工作半年的心路历程
  2. 2014华中首届手游创意大赛
  3. Python uiautomation初探,测试Win10计算器
  4. 5分钟成为车联网专家 | 全球首个C-V2X车联网城市级规模示范应用
  5. ​浅谈 Java 后端开发工程师腾讯面试经历分享总结
  6. 计算机财务管理专业有哪些课程设计,财务管理专业课程设计模版.pdf
  7. 我为什么说【直播电商】是第三代电商?
  8. uniport ID 转换为 gene symbol(ID转换)
  9. 如何存储10亿的数据
  10. Hadoop 容错之1.X的单点故障 到 2.X的HA和HDFS Federation