-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path129.cpp
196 lines (177 loc) · 4.31 KB
/
129.cpp
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
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <complex>
#include <iomanip>
#define rep(i,j,k) for (int i=j;i<=k;++i)
#define rrep(i,j,k) for (int i=j;i>=k;--i)
#define x real()
#define y imag()
using namespace std;
const int maxn = 401;
const long double pi = 3.1415926535897932,INF = 0x7FFFFFFF/2;
typedef complex<long double> point;
struct node{int pos;long double arg;}_node[maxn];
struct edge{point s,t;}_edge[maxn];
long double presion = 1e-12;
point p[maxn],pOrd[maxn],in,out,mid,non,zero,inter[2];
int n,m,sta,interNum;
int dblcmp(long double t)
{
if (fabs(t)<presion) return 0;
return (t>0)?1:-1;
}
long double cross(point p1,point p2){return p1.x*p2.y-p2.x*p1.y;}
bool cmp(const node & a,const node & b){return a.arg<b.arg;}
long double dist(point p1,point p2){return sqrt(fabs(p2.x-p1.x)*fabs(p2.x-p1.x)+
fabs(p2.y-p1.y)*fabs(p2.y-p1.y));}
void theIniter()
{
cin >> n;
zero.x = zero.y = 0;
mid.x = mid.y = 0;
non.x = non.y = -INF;
rep(i,1,n) cin >> p[i].x >> p[i].y,mid+=p[i];
mid.x/=(long double)n,mid.y/=(long double)n;
rep(i,1,n) _node[i].pos = i,_node[i].arg = arg(p[i]-mid);
sort(_node+1,_node+n+1,cmp);
_edge[n].s = p[_node[n].pos],_edge[n].t = p[_node[1].pos];
rep(i,1,n-1) _edge[i].s = p[_node[i].pos],_edge[i].t = p[_node[i+1].pos];
pOrd[1] = p[_node[1].pos];
rep(i,2,n) pOrd[i] = p[_node[i].pos]-pOrd[1];
}
bool intersect(point p,point p1,point p2)
{
if (p.x>=min(p1.x,p2.x) &&
p.x<=max(p1.x,p2.x) &&
p.y>=min(p1.y,p2.y) &&
p.y<=max(p1.y,p2.y)) return true;
return false;
}
int check(point poi)
{
poi-=pOrd[1];
int dd1 = dblcmp(cross(poi,pOrd[2]));
int dd2 = dblcmp(cross(poi,pOrd[n]));
if ((dd1 == 0 && intersect(poi,zero,pOrd[2])) ||
(dd2 == 0 && intersect(poi,zero,pOrd[n])))
return 0;
if (dd1>0 || dd2<0) return (-1);
int l = 3,r = n;
while (l<=r)
{
int mid = (l+r)/2;
int d1 = dblcmp(cross(poi,pOrd[mid-1]));
int d2 = dblcmp(cross(poi,pOrd[mid]));
if (d1<=0)
{
if (d2>=0)
{
int d3=dblcmp(cross(poi-pOrd[mid-1],pOrd[mid]-pOrd[mid-1]));
if(d3<0)
return 1;
if (d3 == 0)
return 0;
break;
}
else l = mid+1;
}
else r = mid-1;
}
return (-1);
}
void thePrinter(long double t)
{
cout << setiosflags(ios::fixed)
<< setprecision(2)
<< t
<< endl;
}
void findInter(point a,point b)
{
point ret;
rep(i,1,n)
{
if (dblcmp(cross(a-p[i],b-p[i]))==0 &&
intersect(p[i],a,b))
inter[interNum++] = p[i];
}
rep(i,1,n)
{
point c = _edge[i].s;
point d = _edge[i].t;
long double s1,s2,s3,s4;
int d1,d2,d3,d4,d5;
d1 = dblcmp(s1 = cross(c-a,d-a));
d2 = dblcmp(s2 = cross(c-b,d-b));
d3 = dblcmp(s3 = cross(a-c,b-c));
d4 = dblcmp(s4 = cross(a-d,b-d));
d5 = d1^d2;
if ((d5 == -2 || d5 == 1 || d5 == -1) && (d3^d4) == -2)
{
long double mul = fabs(s3)/fabs(s4);
ret.x = (c.x+mul*d.x)/(1.0+mul);
ret.y = (c.y+mul*d.y)/(1.0+mul);
inter[interNum++] = ret;
}
}
}
bool sameLine(point in,point out)
{
rep(j,1,n)
{
point c = _edge[j].s;
point d = _edge[j].t;
int d1 = dblcmp(cross(c-in,d-in));
int d2 = dblcmp(cross(c-out,d-out));
if (d1 == 0 && d2 == 0)
{thePrinter(0);return true;}
}
return false;
}
double getDist()
{
interNum = 0;
findInter(in,out);
int check1 = check(in);
int check2 = check(out);
if (interNum == 0)
{
if (check1==1 && check2==1)
return (dist(in,out));
}
else
if (interNum == 1)
{
if (check1==1) return (dist(inter[0],in));
if (check2==1) return (dist(inter[0],out));
}
else
if (interNum == 2)
{
return (dist(inter[0],inter[1]));
}
return 0;
}
void theSolver()
{
cin >> m;
rep(i,1,m)
{
cin >> in.x >> in.y >> out.x >> out.y;
bool che = sameLine(in,out);
if (!che) thePrinter(getDist());
}
}
int main()
{
freopen("test5.in","r",stdin);
freopen("test.out","w",stdout);
ios::sync_with_stdio(false);
theIniter();
theSolver();
}