|
计算几何 叉积法
- #include<iostream>
- #include<iomanip>
- #include<cstdio>
- using namespace std;
- int n,bb;
- double const eps=1e-3;
- double a,b,ans,midx,midy;
- double ly=1,ry=1000000,r[5001],c[5001];
- struct cPoint
- {
- double x,y;
- cPoint(double xx,double yy):x(xx),y(yy) {}
- };
- struct cVector
- {
- double x,y;
- cVector(double xx,double yy): x(xx),y(yy) {}
- };
- cVector operator- (cPoint A,cPoint B)
- {
- return cVector(B.x-A.x,B.y-A.y);
- }
- double operator^ (cVector s,cVector t)
- {
- return s.x*t.y-t.x*s.y;
- }
- int main()
- {
- cin>>n;
- for(int i=1; i<=n; i++)cin>>r[i]>>c[i];
- while(ry-ly>eps)
- {
- midy=(ly+ry)/2.0;
- bb=1;
- double lx=1,rx=r[n];
- while(rx-lx>eps&&bb)
- {
- bb=0;
- midx=(lx+rx)/2.0;
- for(int i=1;i<n&&!bb;i++)
- if(((cPoint(r[i],c[i])-cPoint(midx,midy))^(cPoint(r[i+1],c[i+1])-cPoint(midx,midy)))<-eps)
- {
- if(midx>r[i+1])bb=1;
- else if(midx<r[i])bb=2;
- }
- if(bb==1)rx=midx-eps;
- else if(bb==2)lx=midx+eps;
- }
- if(!bb)
- {
- ans=midy;
- ry=midy-eps;
- }
- else ly=midy+eps;
- }
- printf("%.2f",ans);
- return 0;
- }
复制代码 |
|