格雷厄姆扫描算法
凸包是可以覆盖所有给定数据点的最小封闭区域。
Graham 的 Scan 算法会找到凸包的角点。在该算法中,首先选择最低点。该点是凸包的起点。剩余的 n-1 个顶点从起点开始按逆时针方向排序。如果两个或多个点形成相同的角度,则删除除距起点最远的点之外的所有相同角度的点。
从剩余的点开始,将它们压入堆栈。并逐个从堆栈中取出项目,当堆栈顶点、第二个顶点和新选择的点points[i]的方向不是逆时针时,检查后,将points[i]插入堆栈。
输入和输出
Input:Set of points: {(-7,8), (-4,6), (2,6), (6,4), (8,6), (7,-2), (4,-6), (8,-7),(0,0), (3,-2),(6,-10),(0,-6),(-9,-5),(-8,-2),(-8,0),(-10,3),(-2,2),(-10,4)}
Output:
凸包的边界点是:
(-9, -5) (-10, 3) (-10, 4) (-7, 8) (8, 6) (8, -7) (6, -10)
算法
findConvexHull(points, n)
输入 -点集,点数。
输出 -凸包的边界点。
BeginminY := points[0].y
min := 0
for i := 1 to n-1 do
y := points[i].y
if y < minY or minY = y and points[i].x < points[min].x, then
minY := points[i].y
min := i
done
swap points[0] and points[min]
p0 := points[0]
sort points from points[1] to end
arrSize := 1
for i := 1 to n, do
when i < n-1 and (p0, points[i], points[i+1]) are collinear, do
i := i + 1
done
points[arrSize] := points[i]
arrSize := arrSize + 1
done
if arrSize < 3, then
return cHullPoints
push points[0] into stack
push points[1] into stack
push points[2] into stack
for i := 3 to arrSize, do
while top of stack, item below the top and points[i] is not in
anticlockwise rotation, do
delete top element from stack
done
push points[i] into stack
done
while stack is not empty, do
item stack top element into cHullPoints
pop from stack
done
End
示例
#include<iostream>输出结果#include<stack>
#include<algorithm>
#include<vector>
using namespace std;
struct point { //定义二维平面的点
int x, y;
};
point p0; //习惯了另外两点
point secondTop(stack<point>&stk) {
point tempPoint = stk.top(); stk.pop();
point res = stk.top(); //获取第二个顶部元素
stk.push(tempPoint); //再次推上一个顶部
return res;
}
int squaredDist(point p1, point p2) {
return ((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
}
int direction(point a, point b, point c) {
int val = (b.y-a.y)*(c.x-b.x)-(b.x-a.x)*(c.y-b.y);
if (val == 0)
return 0; //colinear
else if(val < 0)
return 2; //逆时针方向
return 1; //顺时针方向
}
int comp(const void *point1, const void*point2) {
point *p1 = (point*)point1;
point *p2 = (point*)point2;
int dir = direction(p0, *p1, *p2);
if(dir == 0)
return (squaredDist(p0, *p2) >= squaredDist(p0, *p1))?-1 : 1;
return (dir==2)? -1 : 1;
}
vector<point>findConvexHull(point points[], int n) {
vector<point> convexHullPoints;
int minY = points[0].y, min = 0;
for(int i = 1; i<n; i++) {
int y = points[i].y;
//找到最底部或最左边的点
if((y < minY) || (minY == y) && points[i].x < points[min].x) {
minY = points[i].y;
min = i;
}
}
swap(points[0], points[min]); //交换最小点到第 0 个位置
p0 = points[0];
qsort(&points[1], n-1, sizeof(point), comp); //将点从 1 个位置到末尾排序
int arrSize = 1; //用于在修改后的数组中定位项目
for(int i = 1; i<n; i++) {
//当第 i 个和第 (i+1) 个元素的角度相同时,删除点
while(i < n-1 && direction(p0, points[i], points[i+1]) == 0)
i++;
points[arrSize] = points[i];
arrSize++;
}
if(arrSize < 3)
return convexHullPoints; //必须至少有 3 个点,返回空列表。
//创建一个堆栈并在堆栈中添加前三个点
stack<point> stk;
stk.push(points[0]); stk.push(points[1]); stk.push(points[2]);
for(int i = 3; i<arrSize; i++) { //对于剩余的顶点
while(direction(secondTop(stk), stk.top(), points[i]) != 2)
stk.pop(); //当顶部、第二个顶部和第 i 个点不左转时,删除点
stk.push(points[i]);
}
while(!stk.empty()) {
convexHullPoints.push_back(stk.top()); //从堆栈中添加点
stk.pop();
}
}
int main() {
point points[] = {{-7,8},{-4,6},{2,6},{6,4},{8,6},{7,-2},{4,-6},{8,-7},{0,0},
{3,-2},{6,-10},{0,-6},{-9,-5},{-8,-2},{-8,0},{-10,3},{-2,2},{-10,4}};
int n = 18;
vector<point> result;
result = findConvexHull(points, n);
cout << "凸包的边界点是: "<<endl;
vector<point>::iterator it;
for(it = result.begin(); it!=result.end(); it++)
cout << "(" << it->x << ", " <<it->y <<") ";
}
凸包的边界点是:(-9, -5) (-10, 3) (-10, 4) (-7, 8) (8, 6) (8, -7) (6, -10)
以上是 格雷厄姆扫描算法 的全部内容, 来源链接: utcz.com/z/359634.html