您的位置:首页 > 编程语言 > Go语言

POJ2007--Scrambled Polygon

2015-08-16 21:22 579 查看
题目大意:乱序给出凸多边形的顶点坐标,要求按逆时针顺序输出各顶点。给的第一个点一定是(0,0),没有其他点在坐标轴上,没有三点共线的情况。

分析:利用叉积排序。

代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

struct P {
int x, y;
P(){}
P(int x, int y):x(x), y(y) {}
};

P operator -(P p, P q) {
return P(p.x-q.x, p.y-q.y);
}

int operator ^(P p, P q) {
return p.x*q.y-p.y*q.x;
}

bool operator <(P p, P q) {<span style="white-space:pre"> //如果p1^p2 〉0,说明p1经逆时针旋转<180度可以到p2,则 p1 </span><<span style="white-space:pre"> p2</span>
if(((p-P(0, 0))^(q-P(0, 0))) < 0)
return false;
else return true;
}

P ps[60];

int main() {
int n = 0;
while(~scanf("%d%d", &ps
.x, &ps
.y)) {
n++;
}
sort(ps+1, ps+n);
for(int i = 0; i < n; i++)
printf("(%d,%d)\n", ps[i].x, ps[i].y);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: