POJ 2007 Scrambled Polygon (极角排序)

题目:传送门

题意:输入一个凸包的所有顶点,逆时针输出凸包的顶点

 

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#define LL long long
#define mem(i, j) memset(i, j, sizeof(i))
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define pb push_back
#define make make_pair
#define INF INT_MAX
#define inf LLONG_MAX
#define PI acos(-1)
using namespace std;

const int N = 5e4 + 5;

struct Point {
    int x, y;
    Point(int x = 0, int y = 0) : x(x), y(y) { } /// 构造函数
};

typedef Point Vector;
/// 向量+向量=向量, 点+向量=向量
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
///点-点=向量
Vector operator - (Point A, Point B) { return Vector(A.x - B.x, A.y - B.y); }
///向量*数=向量
Vector operator * (Vector A, int p) { return Vector(A.x * p, A.y * p); }
///向量/数=向量
Vector operator / (Vector A, int p) { return Vector(A.x / p, A.y / p); }

const int eps = 1e-10;
int dcmp(int x) {
    if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1;
}

bool operator < (const Point& a, const Point& b) {
    return a.x == b.x ? a.y < b.y : a.x < b.x;
}

bool operator == (const Point& a, const Point &b) {
    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}

int Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; } /// 点积
int Length(Vector A) { return sqrt(Dot(A, A)); } /// 计算向量长度
int Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); } /// 向量A、B夹角
int Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } /// 叉积


Point P[N], Q[N];

bool cmp(Point A, Point B) {
    int tmp = Cross(A - P[0], B - P[0]);
    if(dcmp(tmp) == 0) return Length(A - P[0]) < Length(B - P[0]);
    return dcmp(tmp) >= 0;
}

int main() {

    int n = 0;

    while(scanf("%d %d", &P[n].x, &P[n].y) == 2) n++;

    sort(P + 1, P + n, cmp);

    rep(i, 0, n - 1) printf("(%d,%d)\n", P[i].x, P[i].y);

    return 0;
}

 

posted on 2020-03-04 14:17  Willems  阅读(99)  评论(0编辑  收藏  举报

导航