极角排序(主要用于凸包问题)

名词释义:

在平面内取一个定点O,叫极点,引一条射线Ox,叫做极轴,再选定一个长度单位和角度的正方向(通常取逆时针方向)。对于平面内任何一点M,用ρ表示线段OM的长度(有时也用r表示),θ表示从Ox到OM的角度,ρ叫做点M的极径,θ叫做点M的极角,有序数对 (ρ,θ)就叫点M的极坐标
极角排序有很多种方法而我认为只要会叉积一个就够了 之前长用atan2来写极角排序但是有些题目对数字精确性要高很多,所以用atan2会造成精度的损失,所以还是建议使用叉积的那种办法,毕竟在之后的凸包问题中 你还会遇到使用叉积的时候。

对了我们这里研究的极角在-π~π 上
接下来列举一下这些方法:

所有方法的前提:
定义结构体:

typedef struct P
{
     double x,y;
}P;

1.利用atan2()函数按极角从小到大排序

bool cmp1(P a,P b)//在这里一定要注意 atan2函数y写在逗号前 返回的极角范围为-π~π 
{
    if(atan2(a.y,a.x)==atan2(b.y,b.x))
       return atan2(a.y,a.x)<atan2(b.y,b.x);
    else
       return a.x<b.x;
}

2.利用叉积按极角从小到大排序

double compare(P a,P b,P c)
{
  return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
bool cmp2(P a,P b)
{
    P c;//极点(xx,yy)
    c.x=xx;
    c.y=yy;
    if(compare(c,a,b)==0)
        return a.x<b.x;
     else
     return compare(c,a,b)>0;
    
}

3.利用complex类按极角从大到小排序:(这种方法我也没用过 也不太懂)

bool cmp3(P a,P b)
{
  complex<double> c1(a.x,a.y);
  complex<double> c2(b.x,b.y);
  if(arg(c1)==arg(c2))
       return a.x<b.x;
    return arg(c1)<arg(c2);
}

在这里介绍一下 使用complex需要头文件
而 arg()函数返回的便是极角 范围也是-π~π
4.先按象限从小到大排序 再按极角从小到大排序

int Q(P a)//象限排序一定要加上坐标轴
{
  if(a.x>0&&a.y>=0) return 1;
  if(a.x<=0&&a.y>0) return 2;
  if(a.x<0&&a.y<0) return 3;
  if(a.x>0&&a.y<0) return 4;
}
bool cmp4(P a,P b)//先按象限大小排序 再按极角大小排序
{
  if(Q(a)==Q(b))
  return cmp1(a,b);
  else
    Q(a)<Q(b);
}

本文纯粹个人笔记 未经过自己一个一个套题验证 如有错误还望各位dalao指正包涵。

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要使用分治算法来解决平面凸包问题,可以按照以下步骤进行: 1. 将给定的点集按照 x 坐标进行排序,如果 x 坐标相同,则按照 y 坐标排序。 2. 将点集分成两个子集,每个子集包含大约一半的点。可以选择一个 x 坐标值作为分割线,将点集分成左右两个子集,或者选择中间的点作为分割线。 3. 对左右两个子集分别进行递归处理,得到左右两个子集的凸包。 4. 合并左右两个子集的凸包,得到整个点集的凸包。 在每一次递归中,可以使用 Graham 扫描算法或者快速凸包算法来求解子集的凸包。 以下是一个用 C++ 实现的示例代码: ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Point { int x, y; }; // 按照 x 坐标进行排序,如果 x 坐标相同,则按照 y 坐标排序 bool compareX(Point p1, Point p2) { if (p1.x == p2.x) return p1.y < p2.y; return p1.x < p2.x; } // 按照极角排序极角相同则按照距离原点的距离排序 bool compareAngle(Point p1, Point p2) { int cross = (p1.x - p[0].x) * (p2.y - p[0].y) - (p2.x - p[0].x) * (p1.y - p[0].y); if (cross == 0) return (p1.x - p[0].x) * (p1.x - p[0].x) + (p1.y - p[0].y) * (p1.y - p[0].y) < (p2.x - p[0].x) * (p2.x - p[0].x) + (p2.y - p[0].y) * (p2.y - p[0].y); return cross > 0; } // 计算凸包 vector<Point> convexHull(vector<Point>& points, int left, int right) { if (left == right) { vector<Point> hull; hull.push_back(points[left]); return hull; } int mid = (left + right) / 2; vector<Point> hullLeft = convexHull(points, left, mid); vector<Point> hullRight = convexHull(points, mid + 1, right); int sizeLeft = hullLeft.size(); int sizeRight = hullRight.size(); // 合并左右凸包 int leftmost = 0, rightmost = 0; for (int i = 1; i < sizeLeft; i++) { if (hullLeft[i].x < hullLeft[leftmost].x) leftmost = i; } for (int i = 1; i < sizeRight; i++) { if (hullRight[i].x > hullRight[rightmost].x) rightmost = i; } int lowerLeft = leftmost, lowerRight = rightmost; while (true) { int newLowerLeft = (lowerLeft + sizeLeft - 1) % sizeLeft; int cross = (hullLeft[lowerLeft].x - hullLeft[newLowerLeft].x) * (hullRight[lowerRight].y - hullLeft[newLowerLeft].y) - (hullRight[lowerRight].x - hullLeft[newLowerLeft].x) * (hullLeft[lowerLeft].y - hullLeft[newLowerLeft].y); if (cross < 0) break; lowerLeft = newLowerLeft; } while (true) { int newLowerRight = (lowerRight + 1) % sizeRight; int cross = (hullRight[lowerRight].x - hullLeft[lowerLeft].x) * (hullRight[newLowerRight].y - hullLeft[lowerLeft].y) - (hullRight[newLowerRight].x - hullLeft[lowerLeft].x) * (hullRight[lowerRight].y - hullLeft[lowerLeft].y); if (cross < 0) break; lowerRight = newLowerRight; } vector<Point> hull; int current = lowerLeft; while (current != lowerRight) { hull.push_back(hullLeft[current]); current = (current + 1) % sizeLeft; } hull.push_back(hullLeft[current]); current = lowerRight; while (current != lowerLeft) { hull.push_back(hullRight[current]); current = (current + 1) % sizeRight; } hull.push_back(hullRight[current]); return hull; } vector<Point> convexHull(vector<Point>& points) { int n = points.size(); if (n < 3) { return points; } sort(points.begin(), points.end(), compareX); return convexHull(points, 0, n - 1); } int main() { vector<Point> points = { {0, 3}, {1, 1}, {2, 2}, {4, 4}, {0, 0}, {1, 2}, {3, 1}, {3, 3} }; vector<Point> hull = convexHull(points); for (int i = 0; i < hull.size(); i++) { cout << "(" << hull[i].x << ", " << hull[i].y << ")" << endl; } return 0; } ``` 这段代码使用分治算法来解决平面凸包问题,其中 `convexHull` 函数实现了分治过程,`compareX` 和 `compareAngle` 函数用于排序,`main` 函数中的示例展示了如何使用该函数来求解平面凸包问题。你可以将自己的点集替换到 `points` 数组中进行测试。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值