zoukankan      html  css  js  c++  java
  • poj 1279 Art Gallery (Half Plane Intersection)

    1279 -- Art Gallery

      还是半平面交的问题,要求求出多边形中可以观察到多边形所有边的位置区域的面积。其实就是把每一条边看作有向直线然后套用半平面交。这题在输入的时候应该用多边形的有向面积来判断输入的顺序是顺时针的还是逆时针的。

      对于半平面交问题,要注意最后半平面返回的是多少个点。对于小于3个点的情况应该直接返回结果,避免计算过程中产生错误。

    代码如下:

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <iostream>
      4 #include <algorithm>
      5 #include <vector>
      6 #include <cmath>
      7 
      8 using namespace std;
      9 
     10 struct Point {
     11     double x, y;
     12     Point() {}
     13     Point(double x, double y) : x(x), y(y) {}
     14 } ;
     15 template<class T> T sqr(T x) { return x * x;}
     16 typedef Point Vec;
     17 Vec operator + (Vec a, Vec b) { return Vec(a.x + b.x, a.y + b.y);}
     18 Vec operator - (Vec a, Vec b) { return Vec(a.x - b.x, a.y - b.y);}
     19 Vec operator * (Vec a, double p) { return Vec(a.x * p, a.y * p);}
     20 Vec operator / (Vec a, double p) { return Vec(a.x / p, a.y / p);}
     21 
     22 const double EPS = 1e-8;
     23 const double PI = acos(-1.0);
     24 inline int sgn(double x) { return (x > EPS) - (x < -EPS);}
     25 
     26 inline double dotDet(Vec a, Vec b) { return a.x * b.x + a.y * b.y;}
     27 inline double crossDet(Vec a, Vec b) { return a.x * b.y - a.y * b.x;}
     28 inline double dotDet(Point o, Point a, Point b) { return dotDet(a - o, b - o);}
     29 inline double crossDet(Point o, Point a, Point b) { return crossDet(a - o, b - o);}
     30 inline double vecLen(Vec x) { return sqrt(dotDet(x, x));}
     31 inline double toRad(double deg) { return deg / 180.0 * PI;}
     32 inline double angle(Vec v) { return atan2(v.y, v.x);}
     33 inline Vec vecUnit(Vec x) { return x / vecLen(x);}
     34 inline Vec normal(Vec x) { return Vec(-x.y, x.x) / vecLen(x);}
     35 
     36 const int N = 2222;
     37 struct DLine {
     38     Point p;
     39     Vec v;
     40     double ang;
     41     DLine() {}
     42     DLine(Point p, Vec v) : p(p), v(v) { ang = atan2(v.y, v.x);}
     43     bool operator < (DLine L) const { return ang < L.ang;}
     44 } dl[N];
     45 Point pt[N];
     46 
     47 inline bool onLeft(DLine L, Point p) { return crossDet(L.v, p - L.p) > 0;}
     48 Point dLineIntersect(DLine a, DLine b) {
     49     Vec u = a.p - b.p;
     50     double t = crossDet(b.v, u) / crossDet(a.v, b.v);
     51     return a.p + a.v * t;
     52 }
     53 
     54 struct Poly {
     55     vector<Point> pt;
     56     Poly() { pt.clear();}
     57     ~Poly() {}
     58     Poly(vector<Point> &pt) : pt(pt) {}
     59     Point operator [] (int x) { return pt[x];}
     60     int size() { return pt.size();}
     61     double area() {
     62         double ret = 0.0;
     63         int sz = pt.size();
     64         pt.push_back(pt[0]);
     65         for (int i = 1; i <= sz; i++) ret += crossDet(pt[i], pt[i - 1]);
     66         pt.pop_back();
     67         return fabs(ret / 2.0);
     68     }
     69 } ;
     70 
     71 Poly halfPlane(DLine *L, int n) {
     72     Poly ret = Poly();
     73     sort(L, L + n);
     74     int fi, la;
     75     Point *p = new Point[n];
     76     DLine *q = new DLine[n];
     77     q[fi = la = 0] = L[0];
     78     for (int i = 1; i < n; i++) {
     79         while (fi < la && !onLeft(L[i], p[la - 1])) la--;
     80         while (fi < la && !onLeft(L[i], p[fi])) fi++;
     81         q[++la] = L[i];
     82         if (sgn(crossDet(q[la].v, q[la - 1].v)) == 0) {
     83             la--;
     84             if (onLeft(q[la], L[i].p)) q[la] = L[i];
     85         }
     86         if (fi < la) p[la - 1] = dLineIntersect(q[la - 1], q[la]);
     87     }
     88     while (fi < la && !onLeft(q[fi], p[la - 1])) la--;
     89     if (la < fi) return ret;
     90     p[la] = dLineIntersect(q[la], q[fi]);
     91     for (int i = fi; i <= la; i++) ret.pt.push_back(p[i]);
     92     return ret;
     93 }
     94 
     95 bool isClockwise(Point *pt, int n) {
     96     double sum = 0.0;
     97     pt[n] = pt[0];
     98     Point O = Point(0.0, 0.0);
     99     for (int i = 0; i < n; i++) {
    100         sum += crossDet(O, pt[i], pt[i + 1]);
    101     }
    102     return sum < 0;
    103 }
    104 
    105 int main() {
    106     int T, n;
    107     cin >> T;
    108     while (T-- && cin >> n) {
    109         for (int i = 0; i < n; i++) cin >> pt[i].x >> pt[i].y;
    110         pt[n] = pt[0];
    111         if (isClockwise(pt, n)) for (int i = 0; i < n; i++) dl[i] = DLine(pt[i + 1], pt[i] - pt[i + 1]);
    112         else for (int i = 0; i < n; i++) dl[i] = DLine(pt[i], pt[i + 1] - pt[i]);
    113         Poly tmp = halfPlane(dl, n);
    114         if (tmp.size() >= 3) printf("%.2f
    ", tmp.area());
    115         else puts("0.00");
    116     }
    117     return 0;
    118 }
    View Code

    ——written by Lyon

  • 相关阅读:
    BZOJ1000 A+B Problem
    网络最大流
    树形结构
    BZOJ2521 最小生成树 最小割
    HDU5266 LCA 树链剖分LCA 线段树
    BZOJ3991 寻宝游戏 LCA 虚树 SET
    深度优先搜索DFS
    斯特林数
    Noip2017 普及 T3 Chess
    键盘自动机
  • 原文地址:https://www.cnblogs.com/LyonLys/p/poj_1279_Lyon.html
Copyright © 2011-2022 走看看