极角排序

来源:互联网 发布:网络推广方案 编辑:程序博客网 时间:2024/05/21 13:58
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>using namespace std;struct point {    long long x, y;    int f;//代表在哪一个象限    friend point operator - (point a, point b) {        return (point){a.x-b.x, a.y-b.y};    }};
//利用叉积的正负来作cmp.(即是按逆时针排序).long long cross(point a, point b, point c) {//AB X AC    return (b.x-a.x) * (c.y-a.y) - (b.y-a.y) * (c.x-a.x);}bool cmp(const point &a, const point &b) {//逆时针排序    point origin;    origin.x = origin.y = 0;    return cross(origin, a, b) < 0;}
//利用complex的内建函数#include<complex>#define x real()#define y imag()#include<algorithm>using namespace std;bool cmp(const Point& p1, const Point& p2) {    return arg(p1) < arg(p2);}
//利用arctan计算极角大小。(范围『-180,180』)bool cmp(const Point& p1, const Point& p2) {    return atan2(p1.y, p1.x) < atan2(p2.y, p2.x);}
//利用象限加上极角,叉积bool cmp(const point &a, const point &b) {    //先按象限排序,再按极角排序,再按远近排序    if (a.y == 0 && b.y == 0 && a.x*b.x <= 0)return a.x>b.x;    if (a.y == 0 && a.x >= 0 && b.y != 0)return true;    if (b.y == 0 && b.x >= 0 && a.y != 0)return false;    if (b.y*a.y <= 0)return a.y>b.y;    point one;    one.y = one.x = 0;    return cross(one,a,one,b) > 0 || (cross(one,a,one,b) == 0 && a.x < b.x);}//或者int cal(point a) {//计算象限    if(a.x > 0 && a.y >= 0) return 1;    if(a.x <= 0 && a.y > 0) return 2;    if(a.x < 0 && a.y <= 0) return 3;    if(a.x >= 0 && a.y < 0) return 4;}bool cmp(const point a, const point b) {    //先按象限排序,再按极角排序    if(a.f < b.f) return true;    if(a.f > b.f) return false;    long long tmp = cross(s, a, b);    if(tmp > 0) return true;    return false;}
//求cosA的符号bool ok(point a, point b, point c) {     long long tmp = (b.x-a.x) * (c.x-a.x) + (b.y-a.y) * (c.y - a.y);    if(tmp > 0) return true;    return false;}


                                             
0 0
原创粉丝点击