凸包(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

相关文章

解决IDEA报错:编码GBK的不可映射字符问题

《解决IDEA报错:编码GBK的不可映射字符问题》:本文主要介绍解决IDEA报错:编码GBK的不可映射字符问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录IDEA报错:编码GBK的不可映射字符终端软件问题描述原因分析解决方案方法1:将命令改为方法2:右下jav

MyBatis模糊查询报错:ParserException: not supported.pos 问题解决

《MyBatis模糊查询报错:ParserException:notsupported.pos问题解决》本文主要介绍了MyBatis模糊查询报错:ParserException:notsuppo... 目录问题描述问题根源错误SQL解析逻辑深层原因分析三种解决方案方案一:使用CONCAT函数(推荐)方案二:

Redis 热 key 和大 key 问题小结

《Redis热key和大key问题小结》:本文主要介绍Redis热key和大key问题小结,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、什么是 Redis 热 key?热 key(Hot Key)定义: 热 key 常见表现:热 key 的风险:二、

IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

《IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决》:本文主要介绍IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决,本文分步骤结合实例给大... 目录步骤 1:创建 Maven Web 项目步骤 2:添加 Spring MVC 依赖1、保存后执行2、将新的依赖

Spring 中的循环引用问题解决方法

《Spring中的循环引用问题解决方法》:本文主要介绍Spring中的循环引用问题解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录什么是循环引用?循环依赖三级缓存解决循环依赖二级缓存三级缓存本章来聊聊Spring 中的循环引用问题该如何解决。这里聊

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制

关于MongoDB图片URL存储异常问题以及解决

《关于MongoDB图片URL存储异常问题以及解决》:本文主要介绍关于MongoDB图片URL存储异常问题以及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录MongoDB图片URL存储异常问题项目场景问题描述原因分析解决方案预防措施js总结MongoDB图

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring

解决Maven项目idea找不到本地仓库jar包问题以及使用mvn install:install-file

《解决Maven项目idea找不到本地仓库jar包问题以及使用mvninstall:install-file》:本文主要介绍解决Maven项目idea找不到本地仓库jar包问题以及使用mvnin... 目录Maven项目idea找不到本地仓库jar包以及使用mvn install:install-file基

usb接口驱动异常问题常用解决方案

《usb接口驱动异常问题常用解决方案》当遇到USB接口驱动异常时,可以通过多种方法来解决,其中主要就包括重装USB控制器、禁用USB选择性暂停设置、更新或安装新的主板驱动等... usb接口驱动异常怎么办,USB接口驱动异常是常见问题,通常由驱动损坏、系统更新冲突、硬件故障或电源管理设置导致。以下是常用解决