zju1010判断直线相交,求多边形面积

 #include<iostream>
#include<math.h>
using namespace std;

const int MAX=1001;
const double EPS=1e-10;
struct Point
{
 double x,y;
 Point(double a=0,double b=0):x(a),y(b){}
 bool operator == (Point &right)
 {
  return (fabs(x-right.x)<EPS&&fabs(y-right.y)<EPS);
 }
};

Point vertex[MAX];
Point org;

struct Line
{
 Point sp,ep;
};

double max(double a,double b)
{
 return a>b?a:b;
}
double min(double a,double b)
{
 return a<b?a:b;
}

double xMulti(Point a,Point b,Point c=org)
{
 return((a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x));
}

bool intersect(Line &a,Line &b)
{
 if(max(a.sp.x,a.ep.x)>=min(b.sp.x,b.ep.x)&&
  max(b.sp.x,b.ep.x)>=min(a.sp.x,a.ep.x)&&
  max(a.sp.y,a.ep.y)>=min(b.sp.y,b.ep.y)&&
  max(b.sp.y,b.ep.y)>=min(a.sp.y,a.ep.y)&&
  xMulti(b.sp,a.ep,a.sp)*xMulti(a.ep,b.ep,a.sp)>=0&&
  xMulti(a.sp,b.ep,b.sp)*xMulti(b.ep,a.ep,b.sp)>=0)
  return true;
 else
  return false;
}


int main()
{
 int kase=0,i,j;
 int n,size;
 Line l1,l2;
 while(scanf("%d",&n)!=EOF&&n)
 {
  kase++;
  size=0;
  double area=0;
  bool flag=false;
  for(i=0;i<n;i++)
   scanf("%lf%lf",&vertex[i].x,&vertex[i].y);
  printf("Figure %d: ",kase);
  if(n<3){printf("Impossible/n/n");continue;}
  for(i=0;i<n;i++)
  {
   area+=xMulti(vertex[i],vertex[(i+1)%n]);
   l1.sp=vertex[i];
   l1.ep=vertex[(i+1)%n];
   j=i+2;
   int p=n-3;
   while(p--)
   {
    l2.sp=vertex[j%n];
    l2.ep=vertex[(j+1)%n];
    if(intersect(l1,l2))
    {
     flag=true;
     goto end;
    }
    j++;
   }
  }
end:
  if(flag) printf("Impossible/n/n");
  else printf("%0.2lf/n/n",fabs(area/2));
 }
 return 0;
}

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值