判定多边形面积

C - Area

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit Status

Description

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.

<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.

<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. (0n1000)<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\n",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\n",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\n",cas++,area);
        }
    }
    return 0;
}

FAQ | About Virtual Judge | Forum | Discuss | Open Source Project

时间: 2024-10-10 16:53:39

判定多边形面积的相关文章

凸包,多边形面积,线段在多边形内的判定。

zoj3570  Lott's Seal http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4569 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<algorithm> 5 using namespace std; 6 const double eps=1e-8; 7 const int M=10001

ecnu1624求交集多边形面积

链接 本来在刷hdu的一道题..一直没过,看到谈论区发现有凹的,我这种方法只能过凸多边形的相交面积.. 就找来这道题试下水. 两个凸多边形相交的部分要么没有 要么也是凸多边形,那就可以把这部分单独拿出来极角排序.叉积求面积.这部分的顶点要么p在q内的顶点,要么是q在p内的顶点,要么是两凸多边形的交点. 用到了点在多边形内的判定模板. 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #inclu

多边形面积公式

多边形面积公式 设点顺序 (x1 y1) (x2 y2)    ... (xn yn) 则面积等于 |x1   y1 |      |x2   y2|                  |xn   yn| 0.5 * abs( |            | +   |           | + ...... +   |           | ) |x2   y2 |      |x3   y3|                  |x1   y1| 其中        |x1   y1| |

三角剖分求多边形面积的交 HDU3060

1 //三角剖分求多边形面积的交 HDU3060 2 3 #include <iostream> 4 #include <cstdio> 5 #include <cstring> 6 #include <stack> 7 #include <queue> 8 #include <cmath> 9 #include <algorithm> 10 using namespace std; 11 12 const int max

poj 1654 Area(求多边形面积)

题意:从原点出发向八个方向走,所给数字串每个数字代表一个方向,终点与原点连线,求所得多边形面积: 思路:(性质)共起点的两向量叉积的一半为两向量围成三角形的面积.以此计算每条边首尾两个向量的叉积,求和,除二: #include<cstdio> #include<iostream> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const dou

poj1408——叉积求多边形面积

poj1408——叉积求多边形面积 Fishnet Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1853   Accepted: 1185 Description A fisherman named Etadokah awoke in a very small island. He could see calm, beautiful and blue sea around the island. The previou

地球椭球面上多边形面积量算(C++代码)

昨天突然测试的时候发现以前产品中写的地球椭球面上面积计算的代码有点问题,于是今天就彻底修正,从QGIS中抠出代码来用C++重写了一下,新代码可以比较准确计算椭球面上多边形的面积,这个基础函数对空间量算功能中的面积量测非常重要,在这里共享出来供大家参考甚至直接拿过去用. 头文件如下: /** * @file DistanceArea.h * @brief 椭球面上计算多边形面积的接口文件 * @details * @author zxg * @date 2015年5月15日 * @version

多边形面积模板

HDU 2036 改革春风吹满地 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 20033    Accepted Submission(s): 10256 Problem Description “ 改革春风吹满地, 不会AC没关系; 实在不行回老家, 还有一亩三分地. 谢谢!(乐队奏乐)”话说部分学生心态极好,每天就知道游戏,这次

HDU 3060 多边形面积并

Area2 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1197    Accepted Submission(s): 278 Problem Description 小白近期又被空军特招为飞行员,參与一项实战演习.演习的内容还是轰炸某个岛屿(这次的岛屿非常大,非常大非常大非常大,大到炸弹怎么扔都能全然在岛屿上引爆),看来小白确实是