Determining Polygon Area

Source: Internet
Author: User
Tags cas

C-areaTime limit:3000MS Memory Limit:0KB 64bit IO Format:%lld &%llu SubmitStatus

Description

Jerry, a middle school student, addicts himself to mathematical. Maybe the problems he has thought is really too easy to an expert. But as an amateur, especially as a 15-year-old boy, he had do very well. He's so rolling in thinking the mathematical problem that he's easily to try and solve every problem he met in a Mathemat ical. 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 what 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 is not a satisfied with the solution of such a easy problem. "MMM, if there's a random polygon on the paper and then how can I calculate the area?" he asked himself. Till then, he hadn ' t found out the general rules in calculating the area of a random polygon. He clearly knew that the answer to this question are out of his competence. So he asked a erudite expert, to offer him help. The kind behavior would is 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 N Umber of vertexes in the. (0n) <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, (xi, yi) <tex2html_verbatim_mark>. The starts from the first vertex to the second one, then the second to the third, ... and so On. At last, it closes from the nth vertex to the first one.

The input ends with a empty figure (n = 0<tex2html_verbatim_mark>). And this is not being processed.

Output

As shown below, the output of each figure is should contain the figure number and a colon followed by the area of the Or the string 'impossible'.

If the figure is a polygon, compute it area (accurate to fractional digits). According to the input vertexes, if they cannot form a polygon (that's, 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

50 00 10.5 0.51 11 040 00 11 01 10

Sample Output

Figure 1:0.75figure 2:impossible

#include <stdio.h>#include<string.h>#include<vector>#include<iostream>#include<math.h>#include<algorithm>using namespacestd;Const intmaxn=1100;Const Doubleesp=1e-Ten;structpoint{Doublex, y;} POI[MAXN];structline1{point Head,tail;} LINE[MAXN];DoubleTminDoubleADoubleb) {    returnA<b?a:b;}DoubleTmaxDoubleADoubleb) {    returnA>b?a:b;}BOOLInterConstLine1 &m,ConstLine1 &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; Doubleh,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); returnh*i<=esp&&j*k<=esp;}intMain () {intN; intcas=1;  while(SCANF ("%d", &n)! =EOF) {        if(n==0)             Break; if(cas!=1) puts ("");  for(intI=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(intI=0; i<n;i++) {point u,v; if((i== (n1)) {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=T; Line[i].tail=v; }        BOOLflag=false; BOOLtmp=false;  for(intI=0; i<n;i++){             for(intj=i+2; j<n;j++){                 if(i==0&& (j== (n1)))                    Continue; TMP=Inter (Line[i],line[j]); if(TMP) {flag=true;  Break; }            }            if(flag) Break; }        if(flag) {printf ("Figure %d:impossible\n", cas++); }        Else{                 DoubleArea=0; Doublex1,x2,y1,y2,x0,y0; X0=x1=poi[0].x; Y0=y1=poi[0].y;  for(inti =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;}

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

Determining Polygon Area

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.