POJ--2007(計算幾何之極角排序)

個人理解:極角排序是根據座標系內每一個點與x軸所成的角,逆時針比較,按照角度從小到大的方式排序。

其大意是將點按照與某個其他點連線與x軸所成夾角大小排序 最後順序訪問時 感覺就像轉一圈掃描一樣,它是相對於水平序(橫座標排序)而言的。

按座標與X軸所成的角排個序。

典型的極角排序題:POJ--2007

此題AC代碼:

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
struct point
{
	double x, y;
	double operator ^ (point & v)
	{
		return x*v.y - v.x*y;
	}
};
bool cmp(point p1,point p2)			//兩種比較方法都可 
{
	return (p2^p1) >0;
}
//bool operator < (point p1,point p2)//重載小於號
//{
//	if((p2 ^ p1)>0)
//	return true;
//	else
//	return false;
//}
point ps[60];
int main()
{
	int x, y;
	int n = 0;
	while (cin >> ps[n].x >> ps[n].y)
		++n;
	sort(ps + 1, ps + n,cmp);
	cout << "(0,0)" << endl;
	for (int i = n - 1; i > 0; --i)
	cout << "(" << ps[i].x << ","<< ps[i].y << ")" << endl;
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章