• 计算几何 多边形面积交


     参考网址http://blog.csdn.net/xtulollipop/article/details/52357595

    Description

    给定两个简单多边形,你的任务是判断二者是否有面积非空的公共部分。如下图,(a)中的两个

    矩形只有一条公共线段,没有公共面积。

    这里写图片描述

    在本题中,简单多边形是指不自交(也不会接触自身)、不含重复顶点并且相邻边不共线的多 
    边形。

    注意:本题并不复杂,但有很多看上去正确的算法实际上暗藏缺陷,请仔细考虑各种情况。

    Input 
    输入包含不超过 100 组数据。每组数据包含两行,每个多边形占一行。多边形的格式是:第一 个整数 n 表示顶点的个数 (3<=n<=100),接下来是 n 对整数(x,y) (-1000<=x,y<=1000),即多边 形的各个顶点,按照逆时针顺序排列。

    Output 
    对于每组数据,如果有非空的公共部分,输出”Yes”,否则输出”No”。

    Sample Input

    4 0 0 2 0 2 2 0 2 
    4 2 0 4 0 4 2 2 2 
    4 0 0 2 0 2 2 0 2 
    4 1 0 3 0 3 2 1 2

    Sample Output

    Case 1: No 
    Case 2: Yes

    Hint

      1 #include<cstdio>
      2 #include<cmath>
      3 #include<cstring>
      4 #include<iostream>
      5 #include<algorithm>
      6 #include<cstdlib>
      7 #include<queue>
      8 #include<map>
      9 #include<stack>
     10 #include<set>
     11 
     12 using namespace std;
     13 
     14 const int maxn=555;
     15 const int maxisn=10;
     16 const double eps=1e-8;
     17 const double pi=acos(-1.0);
     18 
     19 int dcmp(double x){
     20     if(x>eps) return 1;
     21     return x<-eps ? -1 : 0;
     22 }
     23 inline double Sqr(double x){
     24     return x*x;
     25 }
     26 struct Point{
     27     double x,y;
     28     Point(){x=y=0;}
     29     Point(double x,double y):x(x),y(y){};
     30     friend Point operator + (const Point &a,const Point &b) {
     31         return Point(a.x+b.x,a.y+b.y);
     32     }
     33     friend Point operator - (const Point &a,const Point &b) {
     34         return Point(a.x-b.x,a.y-b.y);
     35     }
     36     friend bool operator == (const Point &a,const Point &b) {
     37         return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
     38     }
     39     friend Point operator * (const Point &a,const double &b) {
     40         return Point(a.x*b,a.y*b);
     41     }
     42     friend Point operator * (const double &a,const Point &b) {
     43         return Point(a*b.x,a*b.y);
     44     }
     45     friend Point operator / (const Point &a,const double &b) {
     46         return Point(a.x/b,a.y/b);
     47     }
     48     friend bool operator < (const Point &a, const Point &b) {
     49         return a.x < b.x || (a.x == b.x && a.y < b.y);
     50     }
     51     inline double dot(const Point &b)const{
     52         return x*b.x+y*b.y;
     53     }
     54     inline double cross(const Point &b,const Point &c)const{
     55         return (b.x-x)*(c.y-y)-(c.x-x)*(b.y-y);
     56     }
     57 
     58 };
     59 
     60 Point LineCross(const Point &a,const Point &b,const Point &c,const Point &d){
     61     double u=a.cross(b,c),v=b.cross(a,d);
     62     return Point((c.x*v+d.x*u)/(u+v),(c.y*v+d.y*u)/(u+v));
     63 }
     64 double PolygonArea(Point p[],int n){
     65      if(n<3) return 0.0;
     66      double s=p[0].y*(p[n-1].x-p[1].x);
     67      p[n]=p[0];
     68      for(int i=1;i<n;i++){
     69         s+=p[i].y*(p[i-1].x-p[i+1].x);
     70      }
     71      return fabs(s*0.5);
     72 }
     73 double CPIA(Point a[],Point b[],int na,int nb){
     74     Point p[maxisn],temp[maxisn];
     75     int i,j,tn,sflag,eflag;
     76     a[na]=a[0],b[nb]=b[0];
     77     memcpy(p,b,sizeof(Point)*(nb+1));
     78     for(i=0;i<na&&nb>2;++i){
     79         sflag=dcmp(a[i].cross(a[i+1],p[0]));
     80         for(j=tn=0;j<nb;++j,sflag=eflag){
     81             if(sflag>=0) temp[tn++]=p[j];
     82             eflag=dcmp(a[i].cross(a[i+1],p[j+1]));
     83             if((sflag^eflag)==-2)
     84                 temp[tn++]=LineCross(a[i],a[i+1],p[j],p[j+1]);
     85         }
     86         memcpy(p,temp,sizeof(Point)*tn);
     87         nb=tn,p[nb]=p[0];
     88     }
     89     if(nb<3) return 0.0;
     90     return PolygonArea(p,nb);
     91 }
     92 double SPIA(Point a[],Point b[],int na,int nb){
     93     int i,j;
     94     Point t1[4],t2[4];
     95     double res=0.0,if_clock_t1,if_clock_t2;
     96     a[na]=t1[0]=a[0];
     97     b[nb]=t2[0]=b[0];
     98     for(i=2;i<na;i++){
     99         t1[1]=a[i-1],t1[2]=a[i];
    100         if_clock_t1=dcmp(t1[0].cross(t1[1],t1[2]));
    101         if(if_clock_t1<0) swap(t1[1],t1[2]);
    102         for(j=2;j<nb;j++){
    103             t2[1]=b[j-1],t2[2]=b[j];
    104             if_clock_t2=dcmp(t2[0].cross(t2[1],t2[2]));
    105             if(if_clock_t2<0) swap(t2[1],t2[2]);
    106             res+=CPIA(t1,t2,3,3)*if_clock_t1*if_clock_t2;
    107         }
    108     }
    109     return res;
    110     //return PolygonArea(a,na)+PolygonArea(b,nb)-res;
    111 }
    112 
    113 Point a[222],b[222];
    114 Point aa[222],bb[222];
    115 
    116 int main(){
    117     int n1,n2;
    118     int cas=0;
    119     while(scanf("%d",&n1)!=EOF){
    120         for(int i=0;i<n1;i++) scanf("%lf %lf",&a[i].x,&a[i].y);
    121         scanf("%d",&n2);
    122         for(int i=0;i<n2;i++) scanf("%lf %lf",&b[i].x,&b[i].y);
    123 
    124         if(fabs(SPIA(a,b,n1,n2))>eps) printf("Case %d: Yes
    ",++cas);
    125         else printf("Case %d: No
    ",++cas);
    126     }
    127     return 0;
    128 }

     //有一次数据这个代码错了

    把赋值方法换成

    b[0]=point(x3,y3); b[1]=point(x4,y3); b[2]=point(x4,y4); b[3]=point(x3,y4);

  • 相关阅读:
    androidimage: load large bitmap Efficiently
    display log information in terminal
    黑客人物介绍
    Linux 基础入门学习
    网络攻防 第四周学习总结
    网络攻防 第二周学习总结
    网络攻防 第三周学习总结
    SpringBoot入门Demo(Hello Word Boot)
    jquery的json对象与字符串之间转换
    Intellij IDEA导入web项目详解(解决访问的404)
  • 原文地址:https://www.cnblogs.com/Annetree/p/6535294.html
Copyright © 2020-2023  润新知