[POJ2007]Scrambled Polygon(计算几何)

来源:互联网 发布:js倒计时刷新页面 编辑:程序博客网 时间:2024/05/16 10:02

题目描述

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

题解

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

代码

#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>#include<cmath>using namespace std;#define N 105const 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);}
0 0
原创粉丝点击