CF-281C Rectangle Puzzle(凸包+面积)

题意:https://codeforces.com/problemset/problem/281/C

就存个模板

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\Users\13606\Desktop\Input.txt","r",stdin);
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr strcat
 13 #include <string>
 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 #include <cassert>
 21 #include <iomanip>
 22 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 23 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 24 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 25 //******************
 26 clock_t __START,__END;
 27 double __TOTALTIME;
 28 void _MS(){__START=clock();}
 29 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__START)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
 30 //***********************
 31 #define rint register int
 32 #define fo(a,b,c) for(rint a=b;a<=c;++a)
 33 #define fr(a,b,c) for(rint a=b;a>=c;--a)
 34 #define mem(a,b) memset(a,b,sizeof(a))
 35 #define pr printf
 36 #define sc scanf
 37 #define ls rt<<1
 38 #define rs rt<<1|1
 39 typedef pair<int,int> PII;
 40 typedef vector<int> VI;
 41 typedef unsigned long long ull;
 42 typedef long long ll;
 43 typedef double db;
 44 const db E=2.718281828;
 45 const db PI=acos(-1.0);
 46 const ll INF=(1LL<<60);
 47 const int inf=(1<<30);
 48 const db ESP=1e-9;
 49 const int mod=(int)1e9+7;
 50 const int N=(int)1e6+10;
 51 
 52 bool zero(double x){
 53     return (x>0?x:-x)<ESP;
 54 }
 55 struct point
 56 {
 57     double x,y;
 58 };
 59 struct line
 60 {
 61     point a,b;
 62 };
 63 //计算 cross product (P1-P0)x(P2-P0)
 64 double xmult(point p1,point p2,point p0){
 65     return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
 66 }
 67 //计算 dot product (P1-P0).(P2-P0)
 68 double dmult(point p1,point p2,point p0){
 69     return (p1.x-p0.x)*(p2.x-p0.x)+(p1.y-p0.y)*(p2.y-p0.y);
 70 }
 71 //两点距离
 72 double distance(point p1,point p2){
 73     return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
 74 }
 75 bool isIntersected(point s1,point e1, point s2,point e2){
 76     return    (max(s1.x,e1.x) >= min(s2.x,e2.x))&&
 77               (max(s2.x,e2.x) >= min(s1.x,e1.x))&&
 78               (max(s1.y,e1.y) >= min(s2.y,e2.y))&&
 79               (max(s2.y,e2.y) >= min(s1.y,e1.y))&&
 80               (xmult(s1,s2,e1)*xmult(s1,e1,e2)>0)&&
 81               (xmult(s2,s1,e2)*xmult(s2,e2,e1)>0);
 82 }
 83 point intersection(point u1,point u2,point v1,point v2){
 84     point ret=u1;
 85     double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
 86              /((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
 87     ret.x+=(u2.x-u1.x)*t;
 88     ret.y+=(u2.y-u1.y)*t;
 89     return ret;
 90 }
 91 
 92 
 93 //点以原点逆时针旋转α度
 94 point rotate(point p0,double alpha){
 95     alpha=alpha/180*PI;
 96     return {p0.x*cos(alpha)-p0.y*sin(alpha),p0.y*cos(alpha)+p0.x*sin(alpha)};
 97 }
 98 
 99 point Pin[20];
100 int cmp(point a,point b)  ///<p1,p2,...pm>为对其余点按以p0为中心的极角逆时针排序所得的点集(如果有多个点有相同的极角,除了距p0最远的点外全部移除)
101 {
102     if(xmult(a,b,Pin[1])>ESP)
103         return 1;
104     if(fabs(xmult(a,b,Pin[1]))<ESP&&distance(b,Pin[1])-distance(a,Pin[1])>ESP)
105         return 1;
106     return 0;
107 }
108 
109 point Stack[N];
110 int top;
111 void Graham(int n,point p[])
112 {
113     top=3;///栈顶在2,因为凸包的前两个点是不会变了
114     sort(p+2,p+1+n,cmp);
115     Stack[1]=p[1];///压p0p1p2进栈S
116     Stack[2]=p[2];
117     Stack[3]=p[3];
118     for(int i=4;i<=n;i++){
119         while(top>=1&&xmult(p[i],Stack[top],Stack[top-1])>ESP){///有了更好的选择
120             top--;
121         }
122         Stack[++top]=p[i];
123     }
124 }
125 double getArea()
126 {
127     double sum=fabs(xmult(Stack[2],Stack[3],Stack[1]));
128     for(int i=3;i<top;i++)
129         sum+=fabs(xmult(Stack[i],Stack[i+1],Stack[1]));
130     return sum/2.0;
131 }
132 
133 int main()
134 {
135     double w,h,alpha;
136     sc("%lf%lf%lf",&w,&h,&alpha);
137     if(alpha==0||alpha==180)
138     {
139         pr("%.10lf
",w*h);
140         return 0;
141     }
142     if(alpha==90&&w==h)
143     {
144         pr("%.10lf
",w*h);
145         return 0;
146     }
147     line line1[10];
148     point p1={-w/2,h/2};
149     point p2={w/2,h/2};
150     point p3={w/2,-h/2};
151     point p4={-w/2,-h/2};
152     line1[1]={p1,p2};
153     line1[2]={p2,p3};
154     line1[3]={p3,p4};
155     line1[4]={p4,p1};
156 
157     line line2[10];
158     p1=rotate(p1,alpha);
159     p2=rotate(p2,alpha);
160     p3=rotate(p3,alpha);
161     p4=rotate(p4,alpha);
162     line2[1]={p1,p2};
163     line2[2]={p2,p3};
164     line2[3]={p3,p4};
165     line2[4]={p4,p1};
166 
167     int cp=0;
168     for(int i=1;i<=4;++i)
169     {
170         for(int j=1;j<=4;++j)
171         {
172             if(isIntersected(line1[i].a,line1[i].b,line2[j].a,line2[j].b))
173                 Pin[++cp]=intersection(line1[i].a,line1[i].b,line2[j].a,line2[j].b);
174         }
175     }
176 //    for(int i=1;i<=cp;++i)pr("%lf	%lf
",Pin[i].x,Pin[i].y);
177     Graham(cp,Pin);
178     pr("%.10lf
",getArea());
179     return 0;
180 }
181 
182 /**************************************************************************************/
原文地址:https://www.cnblogs.com/--HPY-7m/p/12687822.html