Loading

POJ - 2007 Scrambled Polygon 计算几何 极角排序

给定n个点可以形成一个多边形,按照逆时针的顺序输出这n个点。

考虑用叉乘比较极角

struct Point {
    int x, y;
    Point(int _x = 0,int _y = 0): x(_x),y(_y){}
    Point operator -(const Point& b)const {
        return Point(x - b.x, y - b.y);
    }
    double operator ^(const Point& b)const {
        return x * b.y - y * b.x;
    }
};


int cross(Point A, Point B, Point C) {
    return (B - A) ^ (C - A);
}

vector<Point> p;

bool cmp(const Point a,const  Point b) {
    Point z = Point();
    return cross(z, b, a) < 0;
}

int main() {
    int xx, yy;
    while (~scanf("%d%d", &xx, &yy)) {
        p.push_back(Point(xx, yy));
    }
    sort(p.begin() + 1, p.end(),cmp);
    for (int i = 0; i < p.size(); i++) printf("(%d,%d)\n", p[i].x, p[i].y);
}

 

posted @ 2020-08-17 15:47  MQFLLY  阅读(86)  评论(0编辑  收藏  举报