1. 程式人生 > >POJ 2007 Scrambled Polygon 極角序 水

POJ 2007 Scrambled Polygon 極角序 水

位置 cto 極角 rst ostream con ron %d print

LINK

題意:給出一個簡單多邊形,按極角序輸出其坐標。

思路:水題。對任意兩點求叉積正負判斷相對位置,為0則按長度排序

/** @Date    : 2017-07-13 16:46:17
  * @FileName: POJ 2007 凸包極角序.cpp
  * @Platform: Windows
  * @Author  : Lweleth ([email protected])
  * @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)\n", p[i].x, p[i].y);

    return 0;
}

POJ 2007 Scrambled Polygon 極角序 水