●POJ 2007 Scrambled Polygon

题链:

http://poj.org/problem?id=2007

题解:

计算几何,极角排序

按样例来说,应该就是要把凸包上的i点按 第三像限-第四像限-第一像限-第二像限 的顺序输出。

按 叉积 来排序的确可以A掉,但是显然有错呀。

 

比如这个例子:

0 0

-2 2

-1 -1

1 0

 

正确答案显然应该是:

(0,0)

(-2,2)

(-1,-1)

(1,0)

但是 用叉积 排序后却是这样:

(0,0)

(1,0)

(-2,2)

(-1,-1)

(要用叉积排序的话,按道理来讲应该把像限分成两块分别排序吧(上一块(1,2像限),下一块(3,4像限))。)

而用atan2函数来比较,就可以达到正确答案的效果,可是无奈在POJ上会WA,原因不明~

(疑惑疑惑疑惑疑惑疑惑疑惑疑惑,(莫不是我题读错了?))

代码:

 

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const double eps=1e-8;
int sign(double x){
	if(fabs(x)<=eps) return 0;
	return x<0?-1:1;
}
struct Point{
	double x,y;
	Point(double _x=0,double _y=0):x(_x),y(_y){}
	int Read(){return scanf("%lf%lf",&x,&y);}
}D[70];
int N;
typedef Point Vector;
double operator ^ (Vector A,Vector B){return A.x*B.y-A.y*B.x;}
double Angle(Vector A){
	return atan2(A.y,A.x);
}
bool PAcmp(Point A,Point B){//Polar_Angle_cmp
	//return sign(Angle(A)-Angle(B))<0;
	return sign(A^B)>0;
}
void Bubble_Sort(int l,int r){
	for(int i=l;i<=r;i++)
		for(int j=l;j<i;j++)
			if(PAcmp(D[i],D[j])) swap(D[i],D[j]);
}
int main(){
	freopen("/home/noilinux/Documents/模块学习/计算几何/input","r",stdin);
	while(~D[++N].Read());
	sort(D+2,D+N,PAcmp);
	//Bubble_Sort(2,N-1);
	for(int i=1;i<N;i++)
	printf("(%0.lf,%0.lf)\n",D[i].x,D[i].y);
	return 0;
}

 

  

 

posted @ 2018-01-07 11:38  *ZJ  阅读(136)  评论(0编辑  收藏  举报