码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-08-13 20:17:34      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:codeforces

题意:输入多边形的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);
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:codeforces

原文地址:http://blog.csdn.net/ac_0_summer/article/details/47616853

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!