Poj 2451 Uyuw's Concert (半平面交)

传送带

模板题, 注意的一点,atan2在g++中会超时,在c++中却ac了

#include 
#include
#include
#include
#include
#include
using namespace std;
#define db double
const db eps = 1e-10;
const int maxn = 3e4;

struct Point{
    db x, y;
    Point(db x=0, db y=0): x(x), y(y) {}    //必须有初始化
    bool operator < (const Point& p) const{
        return x < p.x || (x == p.x && y < p.y);
    }
};

typedef Point Vector;
Vector operator + (Vector a, Vector b){ return Point(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, db k){ return Vector(a.x*k, a.y*k);}
Vector operator / (Vector a, db k){ return Vector(a.x/k, a.y/k);}

//inline将函数名为内联函数,不使用栈空间,直接运算,但不能有while,switch等复杂语句
inline db dot(Vector a, Vector b){
    return a.x*b.x + a.y*b.y;
}
inline db cross(Vector a, Vector b){
    return a.x*b.y - b.x*a.y;
}
struct DLine{
    Point p;
    Vector v;
    db ang;
    DLine() {}
    DLine(Point p, Vector v): p(p), v(v) {ang = atan2(v.y, v.x);}	
    bool operator < (const DLine& l) const{   //极角排序
        return ang < l.ang;
    }
};
typedef vector Polygon;
db polygon_area(Polygon &pol){
    int sz = pol.size();
    db res = 0.0;
    for(int i=1; i 0;
}
int HalfplaneIntersection(DLine* l, int n, Polygon &poly){
    sort(l, l+n);
    int tail = 0, head = 0;
    Point *p = new Point[n];
    DLine *nl = new DLine[n];
    nl[0] = l[0];
    for(int i=0; i>n;
    Polygon poly;
    Point p[10] = {Point(0,0), Point(10000, 0), Point(10000, 10000), Point(0, 10000), Point(0,0)};
    for(int k=0; k<4; ++k)
        l[k] = DLine(p[k], p[k+1] - p[k]);
    for(int i=4; i<4+n; ++i){
        cin >>p2.x >>p2.y >>p3.x >>p3.y;
        l[i] = DLine(p2, p3-p2);
    }
    int tmp = HalfplaneIntersection(l, n+4, poly);
    db ans = polygon_area(poly);
    printf("%.1f\n", ans);
    return 0;
}

 

你可能感兴趣的:(计算几何)