标签:acm 计算几何 scrambled polygon poj2007 几种极角排序
Scrambled Polygon题目:http://poj.org/problem?id=2007
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 6513 Accepted: 3092
Output
The output lists the vertices of the given polygon, one vertex per line. Each vertex from the input appears exactly once in the output. The origin (0,0) is the vertex on the first line of the output. The order of vertices in the output will determine a trip taken along the polygon‘s border, in the counterclockwise direction. The output format for each vertex is (x,y) as shown below.
90 10
(60,30)
计算几何,给出一系列点,按照极角排序。
应该属于Graham求凸包的前置题目吧。。。
很基础。注意输入时判定停止。
在注意一下精度,应该就OK了,
说一下极角排序几种方法吧,这些都是看人家Blog。
转自:http://www.cnblogs.com/devtang/archive/2012/02/01/2334977.html
1.利用叉积的正负来作cmp.(即是按逆时针排序).此题就是用这种方法
double cross(point p0,point p1,point p2) { return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y); } // sort排序函数 bool cmp(const point &a, const point &b)//逆时针排序 { point origin; // 设置原点 origin.x = origin.y = 0; return cross(origin,b,a)<EPS; }
2.利用complex的内建函数。
#include<complex> #define x real() #define y imag() #include<algorithm> using namespace std; bool cmp(const Point& p1, const Point& p2) { return arg(p1) < arg(p2); }
3.利用arctan计算极角大小。(范围『-180,180』)
bool cmp(const Point& p1, const Point& p2) { return atan2(p1.y, p1.x) < atan2(p2.y, p2.x); }
4.利用象限加上极角,叉积。
bool cmp(const point &a, const point &b)//先按象限排序,再按极角排序,再按远近排序 { if (a.y == 0 && b.y == 0 && a.x*b.x <= 0)return a.x>b.x; if (a.y == 0 && a.x >= 0 && b.y != 0)return true; if (b.y == 0 && b.x >= 0 && a.y != 0)return false; if (b.y*a.y <= 0)return a.y>b.y; point one; one.y = one.x = 0; return cross(one,a,one,b) > 0 || (cross(one,a,one,b) == 0 && a.x < b.x); }
#include <stdio.h> #include <algorithm> #include <iostream> using namespace std; #define EPS 1e-8 struct point { double x,y; }p[51]; double cross(point p0,point p1,point p2) { return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y); } // sort排序函数 bool cmp(const point &a, const point &b)//逆时针排序 { point origin; // 设置原点 origin.x = origin.y = 0; return cross(origin,b,a)<EPS; } int main() { int i,n; n=0; while( scanf("%lf%lf",&p[n].x,&p[n].y)!=EOF ) { ++n; } // 极角排序,第一个空过去 sort(p+1,p+n,cmp); for(i=0;i<n;++i) printf("(%.0lf,%.0lf)\n",p[i].x,p[i].y); return 0; }
ACM-计算几何之Scrambled Polygon——poj2007
标签:acm 计算几何 scrambled polygon poj2007 几种极角排序
原文地址:http://blog.csdn.net/lttree/article/details/24599161