-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathConvex_Hull
229 lines (186 loc) · 4.98 KB
/
Convex_Hull
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/// convex hull from cp algo , graham scan
/**
struct pt {
double x, y;
pt(){}
pt(double x,double y){
this->x = x ; this->y=y ;
}
pt operator - (const pt &a) const { return pt(x - a.x, y - a.y); }
};
int orientation(pt a, pt b, pt c) {
double v = a.x*(b.y-c.y)+b.x*(c.y-a.y)+c.x*(a.y-b.y);
if (v < 0) return -1; // clockwise
if (v > 0) return +1; // counter-clockwise
return 0;
}
bool cw(pt a, pt b, pt c, bool include_collinear) {
int o = orientation(a, b, c);
return o < 0 || (include_collinear && o == 0);
}
bool collinear(pt a, pt b, pt c) { return orientation(a, b, c) == 0; }
void convex_hull(vector<pt>& a, bool include_collinear = false) {
pt p0 = *min_element(a.begin(), a.end(), [](pt a, pt b) {
return make_pair(a.y, a.x) < make_pair(b.y, b.x);
});
sort(a.begin(), a.end(), [&p0](const pt& a, const pt& b) {
int o = orientation(p0, a, b);
if (o == 0)
return (p0.x-a.x)*(p0.x-a.x) + (p0.y-a.y)*(p0.y-a.y)
< (p0.x-b.x)*(p0.x-b.x) + (p0.y-b.y)*(p0.y-b.y);
return o < 0;
});
if (include_collinear) {
int i = (int)a.size()-1;
while (i >= 0 && collinear(p0, a[i], a.back())) i--;
reverse(a.begin()+i+1, a.end());
}
vector<pt> st;
for (int i = 0; i < (int)a.size(); i++) {
while (st.size() > 1 && !cw(st[st.size()-2], st.back(), a[i], include_collinear))
st.pop_back();
st.push_back(a[i]);
}
a = st;
}
/**/
#include<bits/stdc++.h>
using namespace std;
#define MP make_pair
#define PB push_back
#define nn '\n'
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define UNIQUE(vec) vec.resize(distance(vec.begin(),unique(vec.begin(),vec.end()))) ;
#define all(vec) vec.begin(),vec.end()
#define int long long
#define pii pair<int,int>
#define pdd pair<double,double>
typedef long long LL ;
const int MOD=1e9+7,Base=998244353 ;
const int N=1e6+7 ;
const int INF=1LL*1000*1000*1000*1000*1000*1000+7LL, INF2=(1LL<<62) ;
const double pie=acos(-1.0) ;
const double EPS=1e-9 ;
double w , h , a ;
struct line
{
double a , b , c ;
line(){}
line(pdd A , pdd B)
{
a = B.second - A.second;
b = A.first - B.first;
c = a*(A.first) + b*(A.second);
c = -c ; /// ax + by + c = 0 format
}
};
pdd LineLineIntersection(line A, line B){
/// Line AB represented as a1x + b1y = c1
double a1 = A.a ;
double b1 = A.b ;
double c1 = -A.c ;
/// Line CD represented as a2x + b2y = c2
double a2 = B.a ;
double b2 = B.b ;
double c2 = -B.c ;
double determinant = a1*b2 - a2*b1;
if (determinant == 0){
// The lines are parallel. This is simplified
// by returning a pair of FLT_MAX
return make_pair(INF, INF);
}
else{
double x = (b2*c1 - b1*c2)/determinant;
double y = (a1*c2 - a2*c1)/determinant;
return make_pair(x, y);
}
}
double Radian(double x){
return x*pie/180.0 ;
}
pdd RotatePoint(pdd p, pdd o,double ang)
{
double s = sin(Radian(ang)) ;
double c = cos(Radian(ang)) ;
/// translate point back to origin:
p.first -= o.first ;
p.second -= o.second ;
/// rotate point
double xnew = p.first * c - p.second * s ;
double ynew = p.first * s + p.second * c ;
/// translate point back:
p.first = xnew + o.first ;
p.second = ynew + o.second ;
return p ;
}
double Area(vector<pdd>pts) ;
vector<pdd>Sort(vector<pdd>pts) ;
int32_t main()
{
IOS
double w , h , ang ;
cin>>w>>h>>ang ;
if(ang==180.0 or ang==360.0)
ang=0.0 ;
//if(w<h)swap(w,h) ;
w=.5*w ; h=.5*h ;
vector<pdd>pa , pb ;
vector<line>la , lb ;
pa.PB({w,h}) ;
pa.PB({-w,h}) ;
pa.PB({-w,-h}) ;
pa.PB({w,-h}) ;
for(int i=0;i<4;++i)
{
pb.PB(RotatePoint(pa[i],{0.0,0.0},ang)) ;
}
for(int i=0;i<4;++i)
{
la.PB(line(pa[i],pa[(i+1)%4])) ;
lb.PB(line(pb[i],pb[(i+1)%4])) ;
}
vector<pdd>pts ;
for(line La:la)
{
for(line Lb:lb)
{
pdd p=LineLineIntersection(La,Lb) ;
if(p.first==INF)continue ;
if(abs(p.first)<=w and abs(p.second)<=h)
pts.PB(p) ;
}
}
pts=Sort(pts) ;
/**
for(pdd p:pts)
cout<<p.first<<' '<<p.second<<endl ;
/**/
cout<<setprecision(10)<<fixed<<Area(pts)<<endl ;
return 0 ;
}
vector<pdd>Sort(vector<pdd>pts)
{
vector<pdd>ng , pg ;
for(pdd p:pts)
{
if(p.second<0.0)
ng.PB(p) ;
else
pg.PB(p) ;
}
sort(all(ng)) ;
sort(all(pg)) ;
reverse(all(pg)) ;
for(int i=0;i<pg.size();++i)
ng.PB(pg[i]) ;
return ng ;
}
double Area(vector<pdd>pts)
{
pts.PB(pts[0]) ;
double a=0 ;
for(int i=0;i<pts.size()-1;++i)
a+=pts[i].first*pts[i+1].second-pts[i].second*pts[i+1].first ;
a=a*.5 ; return abs(a) ;
}