zoukankan      html  css  js  c++  java
  • 判定多边形面积

    C - Area
    Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
    Submit Status

    Description

    Download as PDF
     

    Jerry, a middle school student, addicts himself to mathematical research. Maybe the problems he has thought are really too easy to an expert. But as an amateur, especially as a 15-year-old boy, he had done very well. He is so rolling in thinking the mathematical problem that he is easily to try to solve every problem he met in a mathematical way. One day, he found a piece of paper on the desk. His younger sister, Mary, a four-year-old girl, had drawn some lines. But those lines formed a special kind of concave polygon by accident as Fig. 1 shows.

    epsfbox{p2419a.eps}<tex2html_verbatim_mark>
    Fig. 1 The lines his sister had drawn

    ``Great!" he thought, ``The polygon seems so regular. I had just learned how to calculate the area of triangle, rectangle and circle. I'm sure I can find out how to calculate the area of this figure." And so he did. First of all, he marked the vertexes in the polygon with their coordinates as Fig. 2 shows. And then he found the result-0.75 effortless.

    epsfbox{p2419b.eps}<tex2html_verbatim_mark>
    Fig.2 The polygon with the coordinates of vertexes

    Of course, he was not satisfied with the solution of such an easy problem. ``Mmm, if there's a random polygon on the paper, then how can I calculate the area?" he asked himself. Till then, he hadn't found out the general rules on calculating the area of a random polygon. He clearly knew that the answer to this question is out of his competence. So he asked you, an erudite expert, to offer him help. The kind behavior would be highly appreciated by him.

    Input

    The input data consists of several figures. The first line of the input for each figure contains a single integer n<tex2html_verbatim_mark> , the number of vertexes in the figure. (0$ le$n$ le$1000)<tex2html_verbatim_mark> .

    In the following n<tex2html_verbatim_mark> lines, each contain a pair of real numbers, which describes the coordinates of the vertexes, (xiyi)<tex2html_verbatim_mark> . The figure in each test case starts from the first vertex to the second one, then from the second to the third, ...and so on. At last, it closes from the nth vertex to the first one.

    The input ends with an empty figure (n = 0<tex2html_verbatim_mark> ). And this figure not be processed.

    Output

    As shown below, the output of each figure should contain the figure number and a colon followed by the area of the figure or the string ``Impossible".

    If the figure is a polygon, compute its area (accurate to two fractional digits). According to the input vertexes, if they cannot form a polygon (that is, one line intersects with another which shouldn't be adjoined with it, for example, in a figure with four lines, the first line intersects with the third one), just display ``Impossible", indicating the figure can't be a polygon. If the amount of the vertexes is not enough to form a closed polygon, the output message should be ``Impossible" either.

    Print a blank line between each test cases.

    Sample Input

    5
    0 0
    0 1
    0.5 0.5
    1 1
    1 0
    4
    0 0
    0 1
    1 0
    1 1
    0
    

    Sample Output

    Figure 1: 0.75
    
    Figure 2: Impossible
    
    #include<stdio.h>
    #include<string.h>
    #include<vector>
    #include<iostream>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    
    const int maxn=1100;
    const double esp=1e-10;
    struct point{
      double x,y;
    }poi[maxn];
    
    
    struct line1{
        point head,tail;
    }line[maxn];
    
    double tmin(double a,double b){
        return a<b?a:b;
    }
    
    double tmax(double a,double b){
        return a>b?a:b;
    
    }
    bool inter(const line1 &m,const line1 &n){
        point a=m.head;
        point b=m.tail;
        point c=n.head;
        point d=n.tail;
        if(tmin(a.x,b.x)>tmax(c.x,d.x)||
           tmin(a.y,b.y)>tmax(c.y,d.y)||
           tmin(c.x,d.x)>tmax(a.x,b.x)||
           tmin(c.y,d.y)>tmax(a.y,b.y))
            return 0;
        double h,i,j,k;
        h=(b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
        i=(b.x-a.x)*(d.y-a.y)-(b.y-a.y)*(d.x-a.x);
        j=(d.x-c.x)*(a.y-c.y)-(d.y-c.y)*(a.x-c.x);
        k=(d.x-c.x)*(b.y-c.y)-(d.y-c.y)*(b.x-c.x);
    
        return h*i<=esp&&j*k<=esp;
    
    
    }
    
    int main(){
        int n;
        int cas=1;
        while(scanf("%d",&n)!=EOF){
            if(n==0)
                break;
            if(cas!=1)
              puts("");
            for(int i=0;i<n;i++){
                cin>>poi[i].x>>poi[i].y;
            }
             if(n<3){
                    printf("Figure %d: Impossible
    ",cas++);
                    continue;
                }
    
    //            if((fabs(poi[0].x-poi[n-1].x)<esp)&&(fabs(poi[0].y-poi[n-1].y)<esp)){
    //                n--;
    //             }
    
            for(int i=0;i<n;i++){
                point u,v;
                if((i==(n-1))){
                    u.x=poi[i].x;
                    u.y=poi[i].y;
                    v.x=poi[0].x;
                    v.y=poi[0].y;
    
    
                }
                else{
                    u.x=poi[i].x;
                    u.y=poi[i].y;
                    v.x=poi[i+1].x;
                    v.y=poi[i+1].y;
    
                }
               line[i].head=u;
                    line[i].tail=v;
    
    
    
    
            }
            bool flag=false;
            bool tmp=false;
            for(int i=0;i<n;i++){
                for(int j=i+2;j<n;j++){
                     if(i==0&&(j==(n-1)))
                        continue;
                    tmp=inter(line[i],line[j]);
                    if(tmp){
                        flag=true;
                        break;
                    }
                }
                if(flag)
                    break;
            }
    
    
            if(flag){
                printf("Figure %d: Impossible
    ",cas++);
            }
            else{
                     double area=0;
                    double x1,x2,y1,y2,x0,y0;
                    x0=x1=poi[0].x;
                    y0=y1=poi[0].y;
                    for(int i = 1; i <= n; i++)////////////////**************************************
                    {
                        if(i < n){
                            x2=poi[i].x;
                            y2=poi[i].y;
    
                        }
                        else
                        {
                            x2 = x0;
                            y2 = y0;
                        }
                        area += (y1 + y2) * (x2 - x1) * 0.5;
                        x1 = x2;
                        y1 = y2;
                    }////////////////////*****************
      
    if(area<0) area*=-1; printf("Figure %d: %.2f ",cas++,area); } } return 0; }

    FAQ | About Virtual Judge | Forum | Discuss | Open Source Project
  • 相关阅读:
    sql server 数据库优化显示执行计划
    你真的了解SQL的索引吗?
    温习Remoting基础
    sql 百万级数据库优化方案
    MYSQL——查看显示数据库
    输入框【普通输入框,邮箱输入框,验证码输入框,手机号码输入框】的测试用例规范
    一个完整的测试计划包含哪些内容?
    MYSQL——多表查询
    软件测试功能性需求(Functional requirement)+非功能性需求(Nonfunctional requirement)
    【自动化测试01】如何配置Android SDK环境变量
  • 原文地址:https://www.cnblogs.com/13224ACMer/p/5694943.html
Copyright © 2011-2022 走看看