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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
| #include<bits/stdc++.h> #include<iostream> using namespace std; typedef double db; constexpr double eps=1e-5; const double pi=acos(-1); int sign(double k){ if (k>eps) return 1; else if (k<-eps) return -1; return 0; } int dbcmp(double x,double y){ if(y-x>eps) return 1; else if(y-x<-eps) return -1; return 0; } template<typename T>struct point { T x, y; bool operator == (const point &a) const { return (abs(x - a.x) <= eps && abs(y - a.y) <= eps); } bool operator < (const point &a) const { if (abs(x - a.x) <= eps) return y < a.y; return x < a.x; } T length2() const { return (*this) * (*this); } T length() const { return sqrt(length2()); } point unit() { double len = length(); return {x / len, y / len}; } point operator -() const { return { -x, -y}; } point operator + (const point &a) const { return {x + a.x, y + a.y}; } point operator - (const point &a) const { return {x - a.x, y - a.y}; } point operator * (const T k) const { return {k * x, k * y}; } point operator / (const T k) const { return {x / k, y / k}; } T operator * (const point &a) const { return x * a.x + y * a.y; } T operator ^ (const point &a) const { return x * a.y - y * a.x; } point rotate(const double rad) const { double c = cos(rad), s = sin(rad); return {x*c - y * s, x*s + y * c}; } point rotate90() const { return { -y, x}; } double get_angle (const point &a) const { return atan2(y, x); } friend istream & operator >> (istream&, point &a) { scanf("%lf %lf", &a.x, &a.y); return cin; } friend ostream & operator << (ostream&, point &a) { printf(" ( %.6lf , %.6lf ) ", a.x, a.y); return cout; } int toleft (const point &a) const { const auto t = (*this)^a; return (t > eps) - (t < -eps); } };
template<typename T> struct line { point<T> p, v; bool operator == (const line &a) const { return abs(v ^ a.v) <= eps && abs(v ^ (p - a.p)) <= eps; } int toleft(const point<T> &a) const { return v.toleft(a - p); } int is_on(const point<T> &q) const { point<T> pq=p-q; return abs((pq^v))<=eps; } }; template<typename T> struct segment { point<T> a, b; int is_on(const point<T> &p) const { if (p == a || p == b) return -1; return (p - a).toleft(p - b) == 0 && (p - a) * (p - b) < -eps; } int is_inter(const line<T> &l) const { if (l.toleft(a) == 0 || l.toleft(b) == 0) return -1; return l.toleft(a) != l.toleft(b); } int is_inter(const segment<T> &s) const { if (is_on(s.a) || is_on(s.b) || s.is_on(a) || s.is_on(b)) return -1; const line<T> l {a, b - a}, ls {s.a, s.b - s.a}; return l.toleft(s.a) * l.toleft(s.b) == -1 && ls.toleft(a) * ls.toleft(b) == -1; }
double dis(const point<T> &p) const { if ((p - a) * (b - a) < -eps || (p - b) * (a - b) < -eps) return min(p.dis(a), p.dis(b)); const line<T> l {a, b - a}; return l.dis(p); }
double dis(const segment<T> &s) const { if (is_inter(s)) return 0; return min({dis(s.a), dis(s.b), s.dis(a), s.dis(b)}); } }; template<typename T> struct circle { point<T> o; T r; int is_in(const point<T> &q) const { point<T> qo=q-o; return (qo.length()-r)<-eps; } };
int getSegCircleIntersection(line<db> L,circle<db> C, vector<point<db>>& sol) { double a=L.v.x; double b=L.p.x-C.o.x; double c=L.v.y; double d=L.p.y-C.o.y; double e=a*a+c*c; double f=2*(a*b + c*d); double g=b*b+d*d-C.r*C.r; double delta=f*f-4*e*g; double t1,t2; int ans = 0; if(sign(delta)<0) return 0; if(sign(delta)==0) { t1=t2=-f/(2*e); if(sign(t1)>=0&&sign(t1-1)<=0){ ans++; sol.push_back(L.p+L.v*t1); } return ans; } t1=(-f-sqrt(delta))/(2*e); t2=(-f+sqrt(delta))/(2*e); if(t1>t2) swap(t1,t2); if(sign(t1)>=0&&sign(t1-1)<=0){ ans++; sol.push_back(L.p+L.v*t1); } if(sign(t2)>=0&&sign(t2-1)<= 0) { ans++; sol.push_back(L.p+L.v*t2); } return ans; } double TriangleArea(point<db>A,point<db> B,point<db> C) { return (double)(fabs((B-A)^(C-A)) )/ 2; } double Angle(point<db> A, point<db> B) { if(sign(A^B)==0) return 0; return (double)acos(A*B / A.length() / B.length()); } double IntersectionArea(circle<db> C,point<db> A,point<db> B) { if((A==B)|| (B ==C.o)||(C.o==A)) return 0; line<db> L={A,B-A}; int cnt=0; bool inA, inB; if(inA=C.is_in(A)) cnt++; if(inB=C.is_in(B)) cnt++; if(cnt == 2) return TriangleArea(C.o, A, B); if(cnt == 1) { vector<point<db>> q; getSegCircleIntersection(L, C, q); if(inB) swap(A, B); double theta = Angle(q[0]-C.o, B-C.o); return C.r*C.r*theta/2 + TriangleArea(C.o, A, q[0]); }
vector<point<db>> q; int sz = getSegCircleIntersection(L, C, q); if(sz <= 1) { double theta = Angle(A-C.o, B-C.o); return C.r*C.r*theta/2; }
double theta = Angle(C.o-A, C.o-q[0]) + Angle(C.o-B,C.o-q[1]); return C.r*C.r*theta/2 + TriangleArea(C.o,q[0], q[1]); } void solve(){ int n; db k; cin>>n>>k; vector<point<db>> poly(n); for(int i=0;i<n;i++) cin>>poly[i]; point<db> T,S; cin>>T>>S; point<db> M=S*(1/(1-k*k))-T*((k*k)/(1-k*k)); db rr=(S-T).length()*k/(1-k*k); circle<db> C={M,rr};
double ans=0; for(int i=0;i<n;i++){ int sgn; if( sign(((poly[i]-C.o)^(poly[(i+1)%n]-C.o)))>0) sgn = 1; else sgn=-1; ans+=sgn*IntersectionArea(C, poly[i], poly[(i+1)%n]); } printf("%e\n",fabs(ans)); return ; } int main () { #ifdef local freopen("in.txt","r",stdin); freopen("out1.txt","w",stdout); #endif
int t; cin>>t; while(t--) solve(); return 0; }
|