POJ 2007 Scrambled Polygon 极角序 水

LINK

题意:给出一个简单多边形,按极角序输出其坐标。

思路:水题。对任意两点求叉积正负判断相对位置,为0则按长度排序

/** @Date    : 2017-07-13 16:46:17
  * @FileName: POJ 2007 凸包极角序.cpp
  * @Platform: Windows
  * @Author  : Lweleth (SoungEarlf@gmail.com)
  * @Link    : https://github.com/
  * @Version : $Id$
  */
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <stack>
#include <queue>
#include <math.h>
//#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;

struct point
{
	int x, y;
	point(){}
	point(int _x, int _y){x = _x, y = _y;}
	point operator -(const point &b) const
	{
		return point(x - b.x, y - b.y);
	}
	int operator *(const point &b) const 
	{
		return x * b.x + y * b.y;
	}
	int operator ^(const point &b) const
	{
		return x * b.y - y * b.x;
	}
};

double xmult(point p1, point p2, point p0)  
{  
    return (p1 - p0) ^ (p2 - p0);  
}  

double distc(point a, point b)
{
	return sqrt((double)((b - a) * (b - a)));
}

point p[N];

int cmp(point a, point b)
{
	int t = xmult(a, b, p[0]);
	if(t == 0)
		return distc(a, p[0]) < distc(b, p[0]);
	else 
		return t > 0;

}

int main()
{
	int x, y;
	int cnt = 0;
	while(~scanf("%d%d", &x, &y))
	{
		p[cnt++] = point(x, y);
		sort(p + 1, p + cnt, cmp);
	}
	for(int i = 0; i < cnt; i++)
		printf("(%d,%d)
", p[i].x, p[i].y);

    return 0;
}
原文地址:https://www.cnblogs.com/Yumesenya/p/7211794.html