poj_2007_Scrambled Polygon

来源:互联网 发布:淘宝店铺怎么转让 编辑:程序博客网 时间:2024/05/01 09:16

题型:计算几何


题意:给出符合凸包的若干的点,第一个输入的点保证是原点,将这些点从原点开始按逆时针顺序输出。


分析:

        题目中说没有在x轴上和y轴上的点(除了原点),所以极角排序排序的时候,将原点除外之后再排序,否则将得到错误答案(被坑死了,一排WA。。。)

       由于木有说一共输入多少个点,所以用!=EOF处理,用文件输入输出来检测数据。


代码:

#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#define eps 1e-10using namespace std;struct point {    double x,y;} p[100];//极角排序double cross(const point &p1, const point &p2, const point &q1, const point &q2) {    return (q2.y - q1.y)*(p2.x - p1.x) - (q2.x - q1.x)*(p2.y - p1.y);}bool cmp(const point &a, const point &b) {    point origin;    origin.x = origin.y = 0;    return cross(origin,b,origin,a) < 0;}int main() {    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    int n=0;    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("(%.0lf,%.0lf)\n",p[i].x,p[i].y);    }    return 0;}