[POJ2007]Scrambled Polygon(计算几何)

题目描述

传送门
题意:给出一个起点和一个凸多边形,要求从起点开始逆时针输出凸多边形的所有顶点

题解

很多人说是极角序,其实极角应该是向量(x,y)和x轴正方向的夹角,范围是[ π,π ]
我感觉直接极角排序不是很科学,因为起点不确定
所以直接利用叉积的性质来排序就可以了,也就是将所有点和起点求叉积

代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define N 105

const double eps=1e-9;
int dcmp(double x)
{
    if (x<=eps&&x>=-eps) return 0;
    return (x>0)?1:-1;
}
struct Vector
{
    double x,y;
    Vector(double X=0,double Y=0)
    {
        x=X,y=Y;
    }
};
typedef Vector Point;
Vector operator - (Vector A,Vector B) {return Vector(A.x-B.x,A.y-B.y);}

int n;
double x,y;
Point s,p[N];

double Cross(Vector A,Vector B)
{
    return A.x*B.y-A.y*B.x;
}
int cmp(Point a,Point b)
{
    return Cross(a-s,b-s)>0;
}
int main()
{
    scanf("%lf%lf",&x,&y);s=Point(x,y);
    while (~scanf("%lf%lf",&x,&y))
        p[++n]=Point(x,y);
    sort(p+1,p+n+1,cmp);
    printf("(%.0lf,%.0lf)\n",s.x,s.y);
    for (int i=1;i<=n;++i)
        printf("(%.0lf,%.0lf)\n",p[i].x,p[i].y);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值