CSU 1812 三角形和矩形 半平面交模板题

新的独立博客: http://zihengoi.cn 欢迎访问

写在前面

省赛的时候,傻逼了,我把三角形的三个点和矩形的四个点分别按极脚排序后扔到板子里建边然后算,一个人坑了两个小时,最后也没出出来。把队友坑的死死的。。感觉我有罪

题目链接:三角形和矩形

解题思路:

没啥思路,套模板就行。

AC代码:

#include 
using namespace std;

#define pb push_back

typedef complex<double> Point;
typedef pair Halfplane;
typedef vector Convex;

const double EPS = 1e-10;

inline int sgn(double n) { return fabs(n) < EPS ? 0 : (n < 0 ? -1 : 1); }
inline double det(Point a, Point b) { return (conj(a)*b).imag(); }
inline double dot(Point a, Point b) { return (conj(a)*b).real(); }
inline double onLeft(Point a, Halfplane p) {
    return sgn(det(a - p.first, p.second - p.first)) <= 0;
}

Point crossPoint(const Halfplane& a, const Halfplane& b) {
    double k = det(b.first - b.second, a.first - b.second);
    k = k / (k - det(b.first - b.second, a.second - b.second));
    return a.first + (a.second - a.first) * k;
}

bool cmp(const Halfplane& a, const Halfplane& b) {
    int res = sgn(arg(a.second - a.first) - arg(b.second - b.first));
    return res == 0 ? onLeft(a.first, b) : res < 0;
}

vector halfplaneIntersection(vector v) {
    sort(v.begin(), v.end(), cmp);
    deque ans; deque q;
    q.pb(v[0]);
    for(int i = 1; i < int(v.size()); ++i) {
        if(sgn(arg(v[i].second - v[i].first) - arg(v[i-1].second - v[i-1].first)) == 0) continue;
        while(ans.size() > 0 && !onLeft(ans.back(), v[i])) ans.pop_back(), q.pop_back();
        while(ans.size() > 0 && !onLeft(ans.front(), v[i])) ans.pop_front(), q.pop_front();
        ans.pb(crossPoint(q.back(), v[i]));
        q.pb(v[i]);
    }
    while(ans.size() > 0 && !onLeft(ans.back(), q.front())) ans.pop_back(), q.pop_back();
    while(ans.size() > 0 && !onLeft(ans.front(), q.back())) ans.pop_front(), q.pop_front();
    ans.pb(crossPoint(q.back(), q.front()));
    return vector(ans.begin(), ans.end());
}

Convex solve(Convex v1, Convex v2) {
    vector h;
    for(int i = 0; i < v1.size(); ++i) h.pb(Halfplane(v1[i], v1[(i+1) % v1.size()]));
    for(int i = 0; i < v2.size(); ++i) h.pb(Halfplane(v2[i], v2[(i+1) % v2.size()]));
    return halfplaneIntersection(h);
}

double area(vector a) {
    if(a.size() < 3) return 0;
    double sum = 0, n = a.size();
    a.pb(a[0]);
    for(int i = 0; i < n; i++) sum += det(a[i+1], a[i]);
    return fabs(sum/2);
}

int main() {
    Convex tri, rect;
    double x1, y1, x2, y2;
    while(scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2) != EOF) {
        tri.clear(); rect.clear();
        Point t1 = Point(x2, y2);
        if(x1 > x2) swap(x1, x2); if(y1 > y2) swap(y1, y2);
        if(t1 != Point(x1, y1)) tri.pb(Point(x1, y1));
        if(t1 != Point(x2, y1)) tri.pb(Point(x2, y1));
        if(t1 != Point(x2, y2)) tri.pb(Point(x2, y2));
        if(t1 != Point(x1, y2)) tri.pb(Point(x1, y2));
        scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
        if(x1 > x2) swap(x1, x2); if(y1 > y2) swap(y1, y2);
        rect.pb(Point(x1, y1)); rect.pb(Point(x2, y1));
        rect.pb(Point(x2, y2)); rect.pb(Point(x1, y2));
        printf("%.8f\n", area(solve(tri, rect)));
    }
    return 0;
}

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