【CSU1812】三角形和矩形 【半平面交】

检验半平面交的板子。

  1 #include <stdio.h>
  2 #include <bits/stdc++.h>
  3 using namespace std;
  4 #define gg puts("gg");
  5 #define ll long long
  6 const double pi = acos(-1);
  7 const double eps = 1e-9;
  8 int dcmp(double x){
  9     if(fabs(x) < eps) return 0;
 10     return x > 0? 1 : -1;
 11 }
 12 struct Point{
 13     double x, y;
 14     Point(double x = 0, double y = 0):x(x), y(y){}
 15     Point operator +(const Point& a){ return Point(x+a.x, y+a.y);}
 16     Point operator -(const Point& a){ return Point(x-a.x, y-a.y);}
 17     Point operator *(const Point& a){ return Point(x*a.x, y*a.y);}
 18     Point operator *(const double a){ return Point(x*a, y*a); }
 19 };
 20 struct Line{
 21     Point p, v; double a;
 22     Line(){}
 23     Line(Point p, Point v):p(p), v(v){ a = atan2(v.y, v.x);}
 24     bool operator <(const Line& b) const{
 25         return a < b.a;
 26     }
 27 };
 28 
 29 double Cross(const Point& a, const Point& b){
 30     return a.x*b.y-a.y*b.x;
 31 }
 32 bool Onleft(Line L, Point P){
 33     return Cross(L.v, P-L.p) > 0;
 34 }
 35 Point GetIntersection(Line a, Line b){
 36     Point u = a.p-b.p;
 37     double t = Cross(b.v, u)/Cross(a.v, b.v);
 38     return a.p+a.v*t;
 39 }
 40 int Hpi(Line* L, int n, Point* poly, Point* p, Line* q){
 41     sort(L, L+n);
 42     int first, last;
 43     q[first = last = 0] = L[0];
 44     for(int i = 1; i < n; i++){
 45         while(first < last&&!Onleft(L[i], p[last-1])) last--;
 46         while(first < last&&!Onleft(L[i], p[first])) first++;
 47         q[++last] = L[i];
 48         if(fabs(Cross(q[last].v, q[last-1].v)) < eps){
 49             last--;
 50             if(Onleft(q[last], L[i].p)) q[last] = L[i];
 51         }
 52         if(first < last) p[last-1] = GetIntersection(q[last-1], q[last]);
 53     }
 54     while(first < last&&!Onleft(q[first], p[last-1])) last--;
 55     if(last-first <= 1) return 0;
 56     p[last] = GetIntersection(q[last], q[first]);
 57 
 58     int m = 0;
 59     for(int i = first; i <= last; i++)
 60         poly[m++] = p[i];
 61     return m;
 62 }
 63 double PolygonArea(Point* p, int n){
 64     double ans = 0;
 65     for(int i = 1; i < n-1; i++)
 66         ans += Cross(p[i]-p[0], p[i+1]-p[0]);
 67     return ans/2.0;
 68 }
 69 
 70 Line L[100], q[100];
 71 Point p[100], poly[100];
 72 
 73 double x[10], y[10];
 74 int main(){
 75     while(~scanf("%lf%lf", x+1, y+1)){
 76         for(int i = 2; i <= 4; i++)
 77             scanf("%lf%lf", x+i, y+i);
 78         p[0] = Point(x[1], y[1]);
 79         p[1] = Point(x[1], y[2]);
 80         p[2] = Point(x[2], y[1]);
 81         if(Onleft(Line(p[0], p[1]-p[0]), p[2]))
 82             L[0] = Line(p[0], p[1]-p[0]);
 83         else
 84             L[0] = Line(p[1], p[0]-p[1]);
 85 
 86         if(Onleft(Line(p[0], p[2]-p[0]), p[1]))
 87             L[1] = Line(p[0], p[2]-p[0]);
 88         else
 89             L[1] = Line(p[2], p[0]-p[2]);
 90 
 91         if(Onleft(Line(p[1], p[2]-p[1]), p[0]))
 92             L[2] = Line(p[1], p[2]-p[1]);
 93         else
 94             L[2] = Line(p[2], p[1]-p[2]);
 95         ///////////////////////////////////////
 96         L[3] = Line(Point(x[3], y[3]), Point(1, 0));
 97         L[4] = Line(Point(x[4], y[3]), Point(0, 1));
 98         L[5] = Line(Point(x[4], y[4]), Point(-1, 0));
 99         L[6] = Line(Point(x[3], y[4]), Point(0, -1));
100         int tot = Hpi(L, 7, poly, p, q);
101         if(tot == 0) printf("%.8f\n", 0.0);
102         else
103             printf("%.8f\n", PolygonArea(poly, tot));
104     }
105     return 0;
106 }

 

posted @ 2016-11-07 15:39  我在地狱  阅读(322)  评论(0编辑  收藏  举报