Area
题目链接:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1199
Area
总提交: 155 测试通过: 49
描述
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.
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.

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.
输入
The input data consists of several figures. The first line of the input for each figure contains a single integer n, the number of vertexes in the figure. (0 <= n <= 1000).
In the following n lines, each contain a pair of real numbers, which describes the coordinates of the vertexes, (xi, yi). 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). And this figure not be processed.
输出
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.
样例输入
5
0 0
0 1
0.5 0.5
1 1
1 0
4
0 0
0 1
1 0
1 1
0
样例输出
Figure 1: 0.75
Figure 2: Impossible
分析:
题目要我们求出任意多边形的面积。如果要求的多边形是自相交(即不相邻线段有公共点)的多边形或者不能构成三角形则输出Impossible;
在这个题目中我们要解决计算几何中常见的两类问题:1、判断线段相交(在这个题目中线段相交指的是非规范相交);2、求多边形面积(用
有向面积来求)。
代码:
View Code #include<iostream>
#include<iomanip> //c++中保留小数点后几位函数的头文件
#include<cmath>
using namespace std;
const double eps=1e-6;
const int MAXN=1005;
struct Point
{
double x, y;
};
int dblcmp(double d) //精确度
{
if (fabs(d)<eps)
{
return 0;
}
return d>0 ? 1 : -1;
}
double xmult(Point p0, Point p1, Point p2)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
double dmult(Point p0, Point p1, Point p2)
{
return (p2.x-p0.x)*(p1.x-p0.x)+(p2.y-p0.y)*(p1.y-p0.y);
}
int segcross(Point a, Point b, Point c, Point d) //判断线段相交
{
double s1, s2, s3,s4;
int d1, d2, d3, d4;
d1=dblcmp(s1=xmult(a,b,c));
d2=dblcmp(s2=xmult(a,b,d));
d3=dblcmp(s3=xmult(c,d,a));
d4=dblcmp(s4=xmult(c,d,b));
//若规范相交则求交点的代码
if ((d1^d2)==-2 && (d3^d4)==-2) //"(d1^d2)"这个小括号是必须的,否则会错
{
return 1;
}
//判断非规范相交
if (d1==0 && dblcmp(dmult(c,a,b))<=0 ||
d2==0 && dblcmp(dmult(d,a,b))<=0 ||
d3==0 && dblcmp(dmult(a,c,d))<=0 ||
d4==0 && dblcmp(dmult(b,c,d))<=0)
{
return 2;
}
return 0;
}
int main()
{
Point p[MAXN];
int n,count;
for (count=1; cin>>n, n; count++)
{
if (count>1) puts("");
int i,j;
for (i=0; i<n; i++)
{
cin>>p[i].x>>p[i].y;
}
if (n<=2)
{
cout<<"Figure "<<count<<": Impossible"<<endl;
continue;
}
int flag=0; //判断是否有线段相交
for (i=1; i<n-1 && !flag; i++)
{
for (j=i+1; j<n && !flag; j++)
{
if (i-1!=(j+1)%n && segcross(p[i-1], p[i], p[j], p[(j+1)%n])) //p[i-1]与p[(j+1)%n]不可以是同一点
{
flag=1;
}
}
}
if (flag==1)
{
cout<<"Figure "<<count<<": Impossible"<<endl;
continue;
}
double s=0;
Point o;
o.x=o.y=0;
for (i=0; i<n; i++) //求多边形面积
{
s+=xmult(o,p[i],p[(i+1)%n]);
}
cout<<setiosflags(ios::fixed);
cout<<"Figure "<<count<<": "<<setprecision(2)<<fabs(s/2)<<endl;
}
return 0;
}

浙公网安备 33010602011771号