先输入白线的坐标,再输入黑线的坐标,问有能看到白线并且看不到黑线的区域的面积。
线段与线段位置关系分类讨论题。
#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define ms(x, y) memset(x, y, sizeof(x))
#define SZ(x) int(x.size())
#define fk cout<<"fk"<<endl
#define debug(x) cerr<<(#x)<<'='<<(x)<<endl
#define debug2(x,y) cerr<<(#x)<<'='<<(x)<<' '<<(#y)<<'='<<(y)<<endl
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long i64;
template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
void fastio(){ios::sync_with_stdio(false); cin.tie(0); cout.precision(10); cout<<fixed;}
//1.integer overflow (1e5 * 1e5) (2e9 + 2e9)
//2.runtime error (divide ZERO)
//3.boundary condition
const double eps=1e-8;
int sgn(double x){
if(fabs(x) < eps) return 0;
if(x < 0) return -1;
return 1;
}
struct vec{
double x,y;
vec(double _x=0,double _y=0){x=_x,y=_y;}
vec operator + (vec v) {return vec(x+v.x,y+v.y);}
vec operator - (vec v) {return vec(x-v.x,y-v.y);}
bool operator == (vec v) {return (!sgn(x-v.x) && !sgn(y-v.y));}
vec operator * (double v) {return vec(x*v,y*v);}
vec operator / (double v) {return vec(x/v,y/v);}
double operator * (vec v) {return x*v.x + y*v.y;}
double len() {return hypot(x,y);}
double len_sqr() {return x*x + y*y;}
//逆时针旋转
vec rot(double c) {return vec(x*cos(c)-y*sin(c),x*sin(c)+y*cos(c));}
vec trunc(double l) {return (*this) * l / len();}
vec rot90() {return vec(-y,x);}
};
//叉积
double cross(vec a,vec b) {return a.x*b.y - a.y*b.x;}
//判断点是否在线段上(包含端点)
bool point_on_segment(vec p,vec a,vec b){
return sgn( cross(b-a,p-a) ) == 0 && sgn( (p-a)*(p-b) ) <= 0;
}
//判断线段ab,pq间是否有交点
int has_intersection(vec a,vec b,vec p,vec q){
int d1 = sgn( cross(b-a,p-a) ),d2 = sgn( cross(b-a,q-a) );
int d3 = sgn( cross(q-p,a-p) ),d4 = sgn( cross(q-p,b-p) );
if( d1 * d2 < 0 && d3 * d4 < 0) return 1;//有交点,且交点不在端点
if( ( d1 == 0 && point_on_segment(p,a,b) )||
( d2 == 0 && point_on_segment(q,a,b) ) ||
( d3 == 0 && point_on_segment(a,p,q) ) ||
( d4 == 0 && point_on_segment(b,p,q) ))
return -1; //重合或交点在端点上
return 0; //无交点
}
//直线求交点,需保证p!=q,a!=b
int line_intersection(vec a,vec b,vec p,vec q,vec &o,double *t=0){
double U=cross(p-a,q-p);
double D=cross(b-a,q-p);
if(sgn(D) == 0) return sgn(U)==0 ? 2 : 0; //重叠|平行
o = a + (b-a) * (U/D);
if(t) *t = U/D;
return 1;
}
int main() {
fastio();
int a1, a2, b1, b2, c1, c2, d1, d2, tc;
scanf("%d", &tc);
int kase = 0;
while(tc--){
scanf("%d%d%d%d%d%d%d%d", &a1, &a2, &b1, &b2, &c1, &c2, &d1, &d2);
vec v1 = vec(a1, a2);
vec v2 = vec(b1, b2);
vec v3 = vec(c1, c2);
vec v4 = vec(d1, d2);
int res = has_intersection(v1, v2, v3, v4);
if(res == 1) printf("Case %d: 0.000\n", ++kase);
else if(res == -1){ //交点是端点
if(!sgn(cross(v3 - v1, v2 - v1)) && !sgn(cross(v4 - v1, v2 - v1))){
printf("Case %d: 0.000\n", ++kase);
} else if(v1 == v3 || v1 == v4 || v2 == v3 || v2 == v4){
printf("Case %d: inf\n", ++kase);
} else if(point_on_segment(v1, v3, v4) || point_on_segment(v2, v3, v4)){
printf("Case %d: 0.000\n", ++kase);
} else if(point_on_segment(v3, v1, v2) || point_on_segment(v4, v1, v2)){
printf("Case %d: inf\n", ++kase);
}
} else{
vec jiao;
int temp;
temp = line_intersection(v1, v2, v3, v4, jiao);
if(temp == 2) {printf("Case %d: 0.000\n", ++kase); continue;}
if(temp == 1 && point_on_segment(jiao, v1, v2)) {printf("Case %d: inf\n", ++kase); continue;}
if(temp == 1 && point_on_segment(jiao, v3, v4)) {printf("Case %d: 0.000\n", ++kase); continue;}
vec q1 = v1, q2 = v3, w1 = v2, w2 = v4;
int res = has_intersection(q1, q2, w1, w2);
int flag = 0;
if(res == 1){
q2 = v4;
w2 = v3;
flag = 1;
}
temp = line_intersection(q1, q2, w1, w2, jiao);
assert(temp != 2);
if(temp == 0) printf("Case %d: inf\n", ++kase);//平行
else{
if(sgn(cross(v4 - v3, jiao - v3)) != sgn(cross(v4 - v3, v2 - v3)) && !flag){
printf("Case %d: inf\n", ++kase);
} else if(sgn(cross(v4 - v3, jiao - v3)) != sgn(cross(v4 - v3, v1 - v3)) && flag){
printf("Case %d: inf\n", ++kase);
} else{
double ans = fabs(cross(v1 - jiao, v2 - jiao));
ans *= 0.5;
printf("Case %d: %.9f\n", ++kase, ans);
}
}
}
}
return 0;
}