【POJ2007】Scrambled Polygon(点集逆时针排序--极角排序/凸包--只适用于凸多边形)

题目地址:http://poj.org/problem?id=2007

解题思路:


每个点都和(0,0)点连接,构成一个向量,逆时针排序这些向量的极角逐渐增大。用atan2求解误差较大,会wa(;´༎ຶД༎ຶ`)舍弃该方法

排完序后的点集,任取相邻两个点A(在前),B(在后),定有Cross(A,B)>0,故cmp函数可写成:

bool cmp(Point A, Point B)
{
   return Cross(A,B) > 0;
}

注意:此方法只适用于凸多边形,故也可用求凸包的方法解这道题,代码略。

 

ac代码:


极角排序

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int maxn = 1000;
int dcmp(double x)
{
    if(fabs(x) < eps) return 0;
    else return x > 0 ? 1 : -1;
}
struct Point
{
    double x,y;
    Point(int x=0, int y=0):x(x),y(y){}
};
typedef Point Vector;
double Cross(Vector a, Vector b)//外积
{
    return a.x * b.y - a.y * b.x;
}
bool cmp(Point A, Point B)
{
   return Cross(A,B) > 0;
}
int n = 0;
Point p[maxn];
int main()
{
    //freopen("/Users/zhangkanqi/Desktop/11.txt","r",stdin);
    while(~scanf("%lf %lf",&p[n].x, &p[n].y)) n++;
    sort(p+1, p+n, cmp);
    for(int i = 0; i < n; i++)
    {
        printf("(%d,%d)\n", (int)p[i].x, (int)p[i].y);
    }
    return 0;
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值