凸包(Convex Hull)问题求解--Gift-Wrapping 算法

2024-03-11 09:18

本文主要是介绍凸包(Convex Hull)问题求解--Gift-Wrapping 算法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 凸包问题(Convex Hull)求解--卷包裹(Gift-Wrapping) 算法

  1.前言

       最近在做MIT 6.031的问题集0时遇到了要计算凸包的问题,题中提示要用Gift Wrapping算法。作为一个在实际工程中需要应用的求解算法来讲它并不是最好的,因为它有着O(nh)的时间复杂度,但是我们依然可以通过它更好地理解问题的实质。更好地学习和应用这个基本算法。

 2.Convex Hull 问题概述

      百度百科中给出的定义为:

凸包(Convex Hull)是一个计算几何(图形学)中的概念。

在一个实数向量空间V中,对于给定集合X,所有包含X的凸集的交集S被称为X的凸包。X的凸包可以用X内所有点(X1,...Xn)的凸组合来构造.

在二维欧几里得空间中,凸包可想象为一条刚好包著所有点的橡皮圈。

用不严谨的话来讲,给定二维平面上的点集,凸包就是将最外层的点连接起来构成的凸多边形,它能包含点集中所有的点。

     通俗来说,二维的凸包就是在平面上给定的若干个点组成的点集中选取最外围的点,使得他们的连线组成的多边形能够覆盖全部的点。并且这些点应当满足以下两个条件:

(1)组成的凸多边形能够覆盖所有的点

(2)所选取的点数越少越好

MIT 6.031 problem Set 0 中给出了具体的设计规约:

    /*** Given a set of points, compute the convex hull, the smallest convex set that contains all the points * in a set of input points. The gift-wrapping algorithm is one simple approach to this problem, and * there are other algorithms too.* * @param points a set of points with xCoords and yCoords. It might be empty, contain only 1 point, two points or more.* @return minimal subset of the input points that form the vertices of the perimeter of the convex hull*/

3.卷包裹(Gift-Wrapping 算法)

3.1算法思想

该算法的思想为

1、首先选取一个最靠边界的点(例如最左上或最右下,我选的最左上)作为起始点,以这个点为基准开始选择下一个点。

2、遍历点集,考察它们相对于基准点所偏转的角度:(即目标点与基准点连线与当前基准点朝向的方向形成的射线所形成的角度),第一个点所朝向的角度设置为0。其中将以north(正上)方向为基准的顺时针偏转角定义为朝向的角度。在点集中选出偏转角最小的点作为下一个点并将其加入结果点集,同时将基准点设置为该点。

3、重复过程2,直至选取的点为起始点。

需要注意的是:在两个点偏转角度相同时,为保证所选取的点数最少,应该选取与基准点距离更大的点。

3.2代码实现(使用Java实现)

首先将点(Point)定义如下:

public class Point {private final double x;private final double y;/*** Construct a point at the given coordinates.* @param x x-coordinate* @param y y-coordinate*/public Point(double x, double y) {this.x = x;this.y = y;}/*** @return x-coordinate of the point*/public double x() {return x;}/*** @return y-coordinate of the point*/public double y() {return y;}
}

然后是具体的方法:

import java.util.Set;
import java.util.HashSet;
public class TurtleSoup {/*** Given the current direction, current location, and a target location, calculate the Bearing* towards the target point.* * The return value is the angle input to turn() that would point the turtle in the direction of* the target point (targetX,targetY), given that the turtle is already at the point* (currentX,currentY) and is facing at angle currentBearing. The angle must be expressed in* degrees, where 0 <= angle < 360. ** * @param currentBearing current direction as clockwise from north* @param currentX current location x-coordinate* @param currentY current location y-coordinate* @param targetX target point x-coordinate* @param targetY target point y-coordinate* @return adjustment to Bearing (right turn amount) to get to target point,*         must be 0 <= angle < 360*/public static double newCalculateBearingToPoint(double currentBearing, double currentX, double currentY,double targetX, double targetY) {//计算偏转角度double hei=Math.abs(currentY-targetY);double wid=Math.abs(currentX-targetX);double slop = Math.sqrt(hei*hei+wid*wid);double CAngle =Math.toDegrees(Math.asin(wid/slop));double TAngle;if(currentX>=targetX&&currentY>targetY) {TAngle=180+CAngle;}else if(currentX>targetX&&currentY<=targetY) {TAngle=360-CAngle;}else if(currentX<targetX&&currentY>=targetY) {TAngle=180-CAngle;}else if(currentX<=targetX&&currentY<targetY) {TAngle=CAngle;}else {return 359;}return (TAngle>=currentBearing)?(TAngle-currentBearing):(360-(currentBearing-TAngle));}public static double calculateDistance(double currentX,double currentY,double targetX,double targetY) {//计算两点距离double wid = Math.abs(currentX-targetX);double hei = Math.abs(currentY-targetY);return Math.sqrt(wid*wid+hei*hei);}/*** Given a set of points, compute the convex hull, the smallest convex set that contains all the points * in a set of input points. The gift-wrapping algorithm is one simple approach to this problem, and * there are other algorithms too.* * @param points a set of points with xCoords and yCoords. It might be empty, contain only 1 point, two points or more.* @return minimal subset of the input points that form the vertices of the perimeter of the convex hull*/public static Set<Point> convexHull(Set<Point> points) {if(points.size()<=2) {return points;}HashSet<Point> result =new HashSet<Point>();Point tmp=points.iterator().next();Point start = tmp;Point targ = tmp;double angle = 0,a1=0,at=0;for(Point p:points) {if(p.x()<start.x()||p.x()==start.x()&&p.y()>start.y())start = p;}result.add(start);Point ptr = start;while(true) {at=TurtleSoup.newCalculateBearingToPoint(angle, ptr.x(), ptr.y(), targ.x(), targ.y());for(Point q:points) {if(targ==q)continue;a1=TurtleSoup.newCalculateBearingToPoint(angle, ptr.x(), ptr.y(), q.x(), q.y());if(a1<at) {//选择偏转角度最小的targ =q;at=a1;}else if(a1==at) {//选择距离更大的double dist=TurtleSoup.calculateDistance(ptr.x(), ptr.y(), targ.x(), targ.y());double dis1=TurtleSoup.calculateDistance(ptr.x(), ptr.y(), q.x(), q.y());if(dis1>dist) {targ=q;at=a1;}}}if(targ == start)//终止条件break;else {angle=at;result.add(targ);ptr =targ;}}return result;}
}

  3.3时间复杂度分析

该算法时间复杂度为O(nh),其中n为所有点的个数,h为凸包中点的个数。

这篇关于凸包(Convex Hull)问题求解--Gift-Wrapping 算法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/797372

相关文章

linux生产者,消费者问题

pthread_cond_wait() :用于阻塞当前线程,等待别的线程使用pthread_cond_signal()或pthread_cond_broadcast来唤醒它。 pthread_cond_wait() 必须与pthread_mutex 配套使用。pthread_cond_wait()函数一进入wait状态就会自动release mutex。当其他线程通过pthread

问题:第一次世界大战的起止时间是 #其他#学习方法#微信

问题:第一次世界大战的起止时间是 A.1913 ~1918 年 B.1913 ~1918 年 C.1914 ~1918 年 D.1914 ~1919 年 参考答案如图所示

2024.6.24 IDEA中文乱码问题(服务器 控制台 TOMcat)实测已解决

1.问题产生原因: 1.文件编码不一致:如果文件的编码方式与IDEA设置的编码方式不一致,就会产生乱码。确保文件和IDEA使用相同的编码,通常是UTF-8。2.IDEA设置问题:检查IDEA的全局编码设置和项目编码设置是否正确。3.终端或控制台编码问题:如果你在终端或控制台看到乱码,可能是终端的编码设置问题。确保终端使用的是支持你的文件的编码方式。 2.解决方案: 1.File -> S

vcpkg安装opencv中的特殊问题记录(无法找到opencv_corexd.dll)

我是按照网上的vcpkg安装opencv方法进行的(比如这篇:从0开始在visual studio上安装opencv(超详细,针对小白)),但是中间出现了一些别人没有遇到的问题,虽然原因没有找到,但是本人给出一些暂时的解决办法: 问题1: 我在安装库命令行使用的是 .\vcpkg.exe install opencv 我的电脑是x64,vcpkg在这条命令后默认下载的也是opencv2:x6

问题-windows-VPN不正确关闭导致网页打不开

为什么会发生这类事情呢? 主要原因是关机之前vpn没有关掉导致的。 至于为什么没关掉vpn会导致网页打不开,我猜测是因为vpn建立的链接没被更改。 正确关掉vpn的时候,会把ip链接断掉,如果你不正确关掉,ip链接没有断掉,此时你vpn又是没启动的,没有域名解析,所以就打不开网站。 你可以在打不开网页的时候,把vpn打开,你会发现网络又可以登录了。 方法一 注意:方法一虽然方便,但是可能会有

代码随想录算法训练营:12/60

非科班学习算法day12 | LeetCode150:逆波兰表达式 ,Leetcode239: 滑动窗口最大值  目录 介绍 一、基础概念补充: 1.c++字符串转为数字 1. std::stoi, std::stol, std::stoll, std::stoul, std::stoull(最常用) 2. std::stringstream 3. std::atoi, std

人工智能机器学习算法总结神经网络算法(前向及反向传播)

1.定义,意义和优缺点 定义: 神经网络算法是一种模仿人类大脑神经元之间连接方式的机器学习算法。通过多层神经元的组合和激活函数的非线性转换,神经网络能够学习数据的特征和模式,实现对复杂数据的建模和预测。(我们可以借助人类的神经元模型来更好的帮助我们理解该算法的本质,不过这里需要说明的是,虽然名字是神经网络,并且结构等等也是借鉴了神经网络,但其原型以及算法本质上还和生物层面的神经网络运行原理存在

vue同页面多路由懒加载-及可能存在问题的解决方式

先上图,再解释 图一是多路由页面,图二是路由文件。从图一可以看出每个router-view对应的name都不一样。从图二可以看出层路由对应的组件加载方式要跟图一中的name相对应,并且图二的路由层在跟图一对应的页面中要加上components层,多一个s结尾,里面的的方法名就是图一路由的name值,里面还可以照样用懒加载的方式。 页面上其他的路由在路由文件中也跟图二是一样的写法。 附送可能存在

vue+elementui--$message提示框被dialog遮罩层挡住问题解决

最近碰到一个先执行this.$message提示内容,然后接着弹出dialog带遮罩层弹框。那么问题来了,message提示框会默认被dialog遮罩层挡住,现在就是要解决这个问题。 由于都是弹框,问题肯定是出在z-index比重问题。由于用$message方式是写在js中而不是写在html中所以不是很好直接去改样式。 不过好在message组件中提供了customClass 属性,我们可以利用

Visual Studio中,MSBUild版本问题

假如项目规定了MSBUild版本,那么在安装完Visual Studio后,假如带的MSBUild版本与项目要求的版本不符合要求,那么可以把需要的MSBUild添加到系统中,然后即可使用。步骤如下:            假如项目需要使用V12的MSBUild,而安装的Visual Studio带的MSBUild版本为V14。 ①到MSDN下载V12 MSBUild包,把V12包解压到目录(