Gym 100625H 多边形面积-计算几何

题意:输入多边形的n个顶点,现在假设在第一二个顶点连线的中点有一个照相机,这个相机的视角与这条边的夹角是45度,求阴影的面积和多边形总面积的比值。

分析:纯粹的几何题,会用向量求面积、交点、判断点在不在两点之间就行了。熟能生巧。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define eps 1e-10
using namespace std;
const double pi=acos(-1.0);
int t,n;
struct node{
    double x,y;
}a[1005];
node p[1005];
node point1,point2,pp;
double det(node a,node b,node c,node d)
{
    return (a.x-b.x)*(c.y-d.y)-(c.x-d.x)*(a.y-b.y);
}
double dot(node a,node b,node c,node d)
{
    return (a.x-b.x)*(c.x-d.x)+(a.y-b.y)*(c.y-d.y);
}
double getarea(node a[],int n)
{
    double s=0;
    for(int i=1;i<n-1;i++){
        s+=det(a[i],a[0],a[(i+1)%n],a[0]);
    }
    s/=2.0;
    return abs(s);
}
node rate(node a,double rad)
{

    double x=a.x,y=a.y;
    a.x=x*cos(rad)+y*sin(rad);
    a.y=y*cos(rad)-x*sin(rad);
    return a;
}
//node getpoint(node a,node b,node c,node d)
//{
//    double s1=det(c,a,c,b);
//    double s2=det(d,a,d,b);
//    node tmp;
//    tmp.x=(s1*d.x-s2*c.x)/(s1-s2);
//    tmp.y=(s1*d.y-s2*c.y)/(s1-s2);
//    return tmp;
//}
node getpoint ( node p , node v , node q , node w ) {
    node u;
    u.x=p.x-q.x;
    u.y=p.y-q.y;
    double t = (w.x*u.y-u.x*w.y)/(v.x*w.y-w.x*v.y);//det ( w , u ) / det ( v , w );
 //   cout<<w.x<<" "<<w.y<<"     "<<u.x<<" "<<u.y<<"     "<<v.x<<" "<<v.y<<endl;
//	cout<<"t: "<<t<<endl;
	node ans;
    ans.x=p.x+v.x*t;
    ans.y=p.y+v.y*t;
    return ans;
}
int main()
{
    cin>>t;
    while(t--){
        cin>>n;
        for(int i=0;i<n;i++) scanf("%lf%lf",&a[i].x,&a[i].y);
        double s1=getarea(a,n);
        double x0=(a[0].x+a[1].x)/2.0;
        double y0=(a[0].y+a[1].y)/2.0;
        node center;
        center.x=x0,center.y=y0;
        pp.x=a[1].x-a[0].x,pp.y=a[1].y-a[0].y;
//		cout<<"p1: "<<pp.x<<" "<<pp.y<<endl;
        pp=rate(pp,-pi/4);
        a[n]=a[0];
        for(int i=1;i<n;i++){
        	node tmp;
        	tmp.x=pp.x+center.x;
        	tmp.y=pp.y+center.y;
        	node w;
        	w.x=a[i].x-a[i+1].x;
        	w.y=a[i].y-a[i+1].y;       	
            point1=getpoint(center,pp,a[i],w);
			//cout<<i<<": "<<point1.x<<" "<<point1.y<<endl;cout<<"panduan:  "<<dot(point1,a[i],point1,a[i+1])<<endl;
          //  point1=getpoint(a[i],a[i+1],center,tmp);
   //         cout<<i<<": "<<point1.x<<" "<<point1.y<<endl;
            if(dot(point1,a[i],point1,a[i+1])<=0){
                break;
            }
      //      if(i==1) cout<<dot(point1,a[i],point1,a[i+1])<<"lalalal"<<endl;
        }
//		cout<<pp.x<<" "<<pp.y<<endl;
     //   pp=rate(pp,-pi/2);
     	double mp=pp.x;
     	pp.x=pp.y;
     	pp.y=-mp;
//		cout<<"p2: "<<pp.x<<" "<<pp.y<<endl;
        
        for(int i=1;i<n;i++){
        	node tmp;
        	tmp.x=pp.x+center.x;
        	tmp.y=pp.y+center.y;
        	node w;
        	w.x=a[i].x-a[i+1].x;
        	w.y=a[i].y-a[i+1].y;       	
            point2=getpoint(center,pp,a[i],w);
      //      cout<<i<<": "<<point2.x<<" "<<point2.y<<endl;cout<<"panduan:  "<<dot(point2,a[i],point2,a[i+1])<<endl;
            if(dot(point2,a[i],point2,a[i+1])<=0){
                break;
            }
        }
        int tot=0;
        p[tot].x=x0,p[tot++].y=y0;
        p[tot].x=point1.x,p[tot++].y=point1.y;
        for(int i=1;i<=n;i++){
            double tmp=det(center,point1,center,a[i])*det(center,point2,center,a[i]);
            if(tmp<=0) p[tot++]=a[i];
        }
        p[tot].x=point2.x,p[tot++].y=point2.y;
//		cout<<point1.x<<" "<<point1.y<<endl;cout<<point2.x<<" "<<point2.y<<endl;
        double s2=getarea(p,tot);
        printf("%.10f\n",s2/s1);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值