首页 > 代码库 > 多边形面积模板

多边形面积模板

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没关系; 实在不行回老家, 还有一亩三分地。 谢谢!(乐队奏乐)”
话说部分学生心态极好,每天就知道游戏,这次考试如此简单的题目,也是云里雾里,而且,还竟然来这么几句打油诗。 好呀,老师的责任就是帮你解决问题,既然想种田,那就分你一块。 这块田位于浙江省温州市苍南县灵溪镇林家铺子村,多边形形状的一块地,原本是linle 的,现在就准备送给你了。不过,任何事情都没有那么简单,你必须首先告诉我这块地到底有多少面积,如果回答正确才能真正得到这块地。 发愁了吧?就是要让你知道,种地也是需要AC知识的!以后还是好好练吧...
 

 

Input
输入数据包含多个测试实例,每个测试实例占一行,每行的开始是一个整数n(3<=n<=100),它表示多边形的边数(当然也是顶点数),然后是按照逆时针顺序给出的n个顶点的坐标(x1, y1, x2, y2... xn, yn),为了简化问题,这里的所有坐标都用整数表示。 输入数据中所有的整数都在32位整数范围内,n=0表示数据的结束,不做处理。
 

 

Output
对于每个测试实例,请输出对应的多边形面积,结果精确到小数点后一位小数。 每个实例的输出占一行。
 

 

Sample Input
3 0 0 1 0 0 14 1 0 0 1 -1 0 0 -10
 

 

Sample Output
0.52.0
 

 

Author
lcy
 

 

Source
ACM程序设计期末考试(2006/06/07)
 

适用于凹凸多边形、

#include<iostream>#include<cmath>#include<cstdio>using namespace std;#define N 110struct Point{    double x,y;    Point(){}    Point (double x,double y):x(x),y(y){    }    Point operator - (Point p){        return Point(x-p.x,y-p.y);    }    double operator ^ (Point p){        return x*p.y-y*p.x;    }};int n;Point p[N];int main(){    while(scanf("%d",&n),n)    {        for(int i=1;i<=n;i++)        {            scanf("%lf%lf",&p[i].x,&p[i].y);        }        double ans=0;        for(int i=3;i<=n;i++)        {            ans+=(p[i]-p[1])^(p[i-1]-p[1]);        }        printf("%.1f\n",0.5*fabs(ans));    }    return 0;}

 POJ 1654

Area
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 15730 Accepted: 4380

Description

You are going to compute the area of a special kind of polygon. One vertex of the polygon is the origin of the orthogonal coordinate system. From this vertex, you may go step by step to the following vertexes of the polygon until back to the initial vertex. For each step you may go North, West, South or East with step length of 1 unit, or go Northwest, Northeast, Southwest or Southeast with step length of square root of 2. 
For example, this is a legal polygon to be computed and its area is 2.5: 

Input

The first line of input is an integer t (1 <= t <= 20), the number of the test polygons. Each of the following lines contains a string composed of digits 1-9 describing how the polygon is formed by walking from the origin. Here 8, 2, 6 and 4 represent North, South, East and West, while 9, 7, 3 and 1 denote Northeast, Northwest, Southeast and Southwest respectively. Number 5 only appears at the end of the sequence indicating the stop of walking. You may assume that the input polygon is valid which means that the endpoint is always the start point and the sides of the polygon are not cross to each other.Each line may contain up to 1000000 digits.

Output

For each polygon, print its area on a single line.

Sample Input

4582567256244865

Sample Output

000.52
#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define N 1000010char s[N];int dir[10][2]={0,0,1,-1,1,0,1,1,0,-1,0,0,0,1,-1,-1,-1,0,-1,1};int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%s",s+1);        int len=strlen(s+1);        if(len<3)        {            cout<<0<<endl;            continue;        }        __int64 x=0,y=0,area=0;        for(int i=1;i<=len;i++)        {            int tx=x+dir[s[i]-0][0];            int ty=y+dir[s[i]-0][1];            area+=(tx*y-x*ty);           // ^            x=tx;            y=ty;        }        if(area<0) area=-area;        if(area%2==0)            printf("%I64d\n",area/2);        else            printf("%I64d.5\n",area/2);    }    return 0;}

 

多边形面积模板