您的位置:首页 > 其它

poj 1654(计算几何)(差乘运用)

2016-05-13 20:32 459 查看
任意多边形的面积可由任意一点与多边形上依次两点连线构成的三角形矢量面积求和得出。

证明: http://www.cnblogs.com/vbspine/archive/2013/03/28/2987818.html

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
typedef long long LL;
const int N = 1000500;
char direction[][2] = { -1,-1,0,-1,1,-1,-1,0,0,0,1,0,-1,1,0,1,1,1 };
char dir
;
int polygon
[2];//储存坐标
int len;
LL Area_Triangle(int k,int d)
{
return polygon[k][0] * polygon[d][1] - polygon[k][1] * polygon[d][0];
}
LL Area()
{
LL s = 0;
for (int i = 0; i < len - 1; i++)
{
s += Area_Triangle(i, i + 1);
}
s += Area_Triangle(len - 1, 0);
if (s > 0)
return s;
else
return -1*s;
}
int main()
{
int t;
#ifdef glx
freopen("in.txt", "r", stdin);
#endif
scanf("%d", &t);
while (t--)
{
scanf("%s", dir);
len = strlen(dir);
polygon[0][0] = 0;
polygon[0][1] = 1;
for (int i = 1; i < len; i++)
{
int num = dir[i-1] - '1';
polygon[i][0] = polygon[i - 1][0] + direction[num][0];
polygon[i][1] = polygon[i - 1][1] + direction[num][1];
}
LL area = Area();
if (area % 2 == 0)
printf("%I64d\n", area / 2);
else printf("%I64d.5\n", area / 2);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: