zoukankan      html  css  js  c++  java
  • poj1474Video Surveillance(半平面交)

    链接

    半平面交的模板题,判断有没有核。;

    注意一下最后的核可能为一条线,面积也是为0的,但却是有的。

      1 #include<iostream>
      2 #include <stdio.h>
      3 #include <math.h>
      4 #define eps 1e-8
      5 using namespace std;
      6 const int MAXN=210;
      7 int m;
      8 double r;
      9 int cCnt,curCnt;//此时cCnt为最终切割得到的多边形的顶点数、暂存顶点个数
     10 struct point
     11 {
     12     double x,y;
     13 };
     14 point points[MAXN],p[MAXN],q[MAXN];//读入的多边形的顶点(顺时针)、p为存放最终切割得到的多边形顶点的数组、暂存核的顶点
     15 void getline(point x,point y,double &a,double &b,double   &c) //两点x、y确定一条直线a、b、c为其系数
     16 {
     17     a = y.y - x.y;
     18     b = x.x - y.x;
     19     c = y.x * x.y - x.x * y.y;
     20 }
     21 void initial()
     22 {
     23     for(int i = 1; i <= m; ++i)p[i] = points[i];
     24     p[m+1] = p[1];
     25     p[0] = p[m];
     26     cCnt = m;//cCnt为最终切割得到的多边形的顶点数,将其初始化为多边形的顶点的个数
     27 }
     28 point intersect(point x,point y,double a,double b,double c) //求x、y形成的直线与已知直线a、b、c、的交点
     29 {
     30     double u = fabs(a * x.x + b * x.y + c);
     31     double v = fabs(a * y.x + b * y.y + c);
     32     point pt;
     33     pt.x=(x.x * v + y.x * u) / (u + v);
     34     pt.y=(x.y * v + y.y * u) / (u + v);
     35     return  pt;
     36 }
     37 void cut(double a,double b ,double c)
     38 {
     39     curCnt = 0;
     40     for(int i = 1; i <= cCnt; ++i)
     41     {
     42         if(a*p[i].x + b*p[i].y + c >= 0)q[++curCnt] = p[i];// c由于精度问题,可能会偏小,所以有些点本应在右侧而没在,
     43         //故应该接着判断
     44         else
     45         {
     46             if(a*p[i-1].x + b*p[i-1].y + c > 0) //如果p[i-1]在直线的右侧的话,
     47             {
     48                 //则将p[i],p[i-1]形成的直线与已知直线的交点作为核的一个顶点(这样的话,由于精度的问题,核的面积可能会有所减少)
     49                 q[++curCnt] = intersect(p[i],p[i-1],a,b,c);
     50             }
     51             if(a*p[i+1].x + b*p[i+1].y + c > 0) //原理同上
     52             {
     53                 q[++curCnt] = intersect(p[i],p[i+1],a,b,c);
     54             }
     55         }
     56     }
     57     for(int i = 1; i <= curCnt; ++i)p[i] = q[i];//将q中暂存的核的顶点转移到p中
     58     p[curCnt+1] = q[1];
     59     p[0] = p[curCnt];
     60     cCnt = curCnt;
     61 }
     62 int dcmp(double x)
     63 {
     64     if(fabs(x)<eps) return 0;
     65     else return x<0?-1:1;
     66 }
     67 void solve()
     68 {
     69     //注意:默认点是顺时针,如果题目不是顺时针,规整化方向
     70     initial();
     71     for(int i = 1; i <= m; ++i)
     72     {
     73         double a,b,c;
     74         getline(points[i],points[i+1],a,b,c);
     75         cut(a,b,c);
     76     }
     77     /*
     78       如果要向内推进r,用该部分代替上个函数
     79       for(int i = 1; i <= m; ++i){
     80           Point ta, tb, tt;
     81           tt.x = points[i+1].y - points[i].y;
     82           tt.y = points[i].x - points[i+1].x;
     83           double k = r / sqrt(tt.x * tt.x + tt.y * tt.y);
     84           tt.x = tt.x * k;
     85           tt.y = tt.y * k;
     86           ta.x = points[i].x + tt.x;
     87           ta.y = points[i].y + tt.y;
     88           tb.x = points[i+1].x + tt.x;
     89           tb.y = points[i+1].y + tt.y;
     90           double a,b,c;
     91           getline(ta,tb,a,b,c);
     92           cut(a,b,c);
     93       }*/
     94     //多边形核的面积
     95     double area = 0;
     96     for(int i = 1; i <= curCnt; ++i)
     97         area += p[i].x * p[i + 1].y - p[i + 1].x * p[i].y;
     98     area = fabs(area / 2.0);
     99     if(!curCnt&&dcmp(area)==0)
    100     puts("Surveillance is impossible.");
    101     else
    102     puts("Surveillance is possible.");
    103 
    104 }
    105 /*void GuiZhengHua(){
    106      //规整化方向,逆时针变顺时针,顺时针变逆时针
    107     for(int i = 1; i < (m+1)/2; i ++)
    108       swap(points[i], points[m-i]);
    109 }*/
    110 int main()
    111 {
    112     int kk = 0;
    113     while(scanf("%d",&m)&&m)
    114     {
    115         int i;
    116         for(i=1; i<=m; i++)
    117             cin>>points[i].x>>points[i].y;
    118         points[m+1]=points[1];
    119         printf("Floor #%d
    ",++kk);
    120         solve();
    121         puts("");
    122     }
    123 }
    View Code
  • 相关阅读:
    绕口令系列 1
    毕业论文排版
    使用matlab表示“段数不确定”的分段函数
    [转]C/C++关于全局变量和局部变量初始化与不初始化的区别
    [转]基于Protel DXP软件的PCB高级编辑技巧大全
    冒泡排序及其优化
    gcc编译器参数
    [转]跟我一起写Makefile系列
    实例说明optimize table在优化mysql时很重要
    log4php0.9的详细配备实例说明
  • 原文地址:https://www.cnblogs.com/shangyu/p/3849286.html
Copyright © 2011-2022 走看看