NC221232. 小圆前辈的魔法
描述
输入描述
前三行每行两个整型数,代表三角形寝室的坐标
第四行四个整型数,代表边界的坐标
输出描述
输出一行浮点数,代表小圆前辈好友得到的无虫区域面积
输出与答案误差不超过 1e-6 视为正确答案
示例1
输入:
0 0 0 2 2 0 -1 -1 2 2
输出:
1.0000000000
说明:
C++(clang++11) 解法, 执行用时: 7ms, 内存消耗: 512K, 提交时间: 2021-04-25 03:28:24
#include<bits/stdc++.h> using namespace std; const double eps=1e-6; struct point{ double x,y; point(){} point(double X,double Y){ x=X;y=Y; } }; struct line{ point a,b; line(point X,point Y){ a=X;b=Y; } }; double xmult(point p1,point p2,point p3){return (p1.x-p3.x)*(p2.y-p3.y)-(p2.x-p3.x)*(p1.y-p3.y);} double area(point p1,point p2,point p3){return fabs(xmult(p1,p2,p3))/2;} double dot_mul(point p1,point p2,point p0){return (p1.x-p0.x) * (p2.y-p0.y) - (p2.x-p0.x) * (p1.y-p0.y);} bool dot_online_in(point a,line b){return fabs(dot_mul(a,b.a,b.b))<eps&&a.x>=min(b.a.x,b.b.x)&&a.x<=max(b.a.x,b.b.x)&&a.y>=min(b.a.y,b.b.y)&&a.y<=max(b.a.y,b.b.y)!=0;} point intersection(line u,line v){ point ret=u.a; double t=((u.a.x-v.a.x)*(v.a.y-v.b.y)-(u.a.y-v.a.y)*(v.a.x-v.b.x))/((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x)); ret.x+=(u.b.x-u.a.x)*t; ret.y+=(u.b.y-u.a.y)*t; return ret; } int main(){ point a,b,c,d,e; double x,y; cin>>x>>y; a={x,y}; cin>>x>>y; b={x,y}; cin>>x>>y; c={x,y}; double areaSum=area(a,b,c); cin>>x>>y; d=point(x,y); cin>>x>>y; e=point(x,y); line l={d,e}; if(dot_online_in(a,l)){ point f=intersection(l,line(b,c)); double area2=area(f,a,c); printf("%.10lf\n",min(areaSum-area2,area2)); } else if(dot_online_in(b,l)){ point f=intersection(l,line(a,c)); double area2=area(f,b,c); printf("%.10lf\n",min(areaSum-area2,area2)); } else if(dot_online_in(c,l)){ point f=intersection(l,line(a,b)); double area2=area(f,c,a); printf("%.10lf\n",min(areaSum-area2,area2)); } else{ double area2; if(xmult(a,l.a,l.b)*xmult(b,l.a,l.b)>eps){ point xx1=intersection(l,line(a,c)); point xx2=intersection(l,line(b,c)); area2=area(xx1,xx2,c); } else if(xmult(b,l.a,l.b)*xmult(c,l.a,l.b)>eps){ point xx1=intersection(l,line(a,c)); point xx2=intersection(l,line(a,b)); area2=area(xx1,xx2,a); } else if(xmult(c,l.a,l.b)*xmult(a,l.a,l.b)>eps){ point xx1=intersection(l,line(a,b)); point xx2=intersection(l,line(b,c)); area2=area(xx1,xx2,b); } printf("%.10lf\n",min(areaSum-area2,area2)); } return 0; }