Scrambled Polygon - POJ 2007(求凸包)

本文介绍了一种基于极坐标的简单算法,用于对给定的一组凸包顶点进行排序。该算法选取其中一个顶点作为起始点,并按顺时针方向对其他顶点进行排序,最终得到有序的顶点序列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给一些点,这些点都是一个凸包上的顶点,以第一个点为起点顺时针把别的点拍排一下序列。

 

分析:最简单的极坐标排序了.....................

代码如下:

--------------------------------------------------------------------------------------------------------------------------

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<string>
#include<vector>
#include<math.h>
using namespace std;

const double EPS = 1e-10;
const double PI = acos(-1);
const int MAXN = 1e3+7;
int sta[MAXN], top;
int Sign(double t)
{
    if(t > EPS)return 1;
    if(fabs(t) < EPS)return 0;
    return -1;
}
struct point
{
    double x, y;
    point(double x=0, double y=0):x(x), y(y){}
    point operator - (const point &t)const{
        return point(x-t.x, y-t.y);
    }
    double operator ^(const point &t)const{
        return x*t.y - y*t.x;
    }
    double operator *(const point &t)const{
        return x*t.x + y*t.y;
    }
}p[MAXN];
double Dist(point a, point b)
{
    return sqrt((a-b)*(a-b));
}
bool cmp(point a, point b)
{
    int t = Sign((a-p[0])^(b-p[0]));

    if(t == 0)
        return Dist(a, p[0]) < Dist(b, p[0]);
    return t > 0;
}

int main()
{
    int i=0, N;

    while(scanf("%lf%lf", &p[i].x, &p[i].y) != EOF)
        i++;
    
    N = i;
    sort(p+1, p+N, cmp);

    for(i=0; i<N; i++)
        printf("(%.0f,%.0f)\n", p[i].x, p[i].y);

    return 0;
}

 

转载于:https://www.cnblogs.com/liuxin13/p/4905457.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值