POJ 1654 Area 多边形面积 三角形面积等于两边的向量的叉积的1/2

问题描述: 多边形面积问题(1-9表示各个方向)

  North  
 789 
West456East
 123 
  South  

 

思路:将多边形两两相邻的点与原点相连组成多个三角形然后求三角形面积

三角形面积等于两边的向量的叉积 :

  设边向量 A(x1, y1)  B(x2, y2)

  则面积Area = 1/2(x1*y2 - y1*y2)

 

11068134NY_lv101654Accepted940K32MSC++861B2012-12-01 14:14:38

 

View Code
 1 /*根据面积公式,Area=1/2*abs((x0*y1-x1*y0)+(x1*y2-x2*y1)...+(xn*yn-1-xn-1*yn)+(xn*y0-x0*yn)) 
 2 把相邻两点和原点组成一个三角形,而总面积是这n个三角形面积的和,而三角形面积是两个相邻边向量的叉积*/
 3 
 4 
 5 #include <iostream>
 6 
 7 using namespace std;
 8 
 9 int dx[] = {0, -1, 0, 1, -1, 0, 1, -1, 0, 1};
10 int dy[] = {0, 1, 1, 1, 0, 0, 0, -1, -1, -1};
11 
12 __int64 area;
13 char cstr[1000005];
14 
15 int main()
16 {
17     int t;
18     string str;
19     int x, y, tx, ty;
20     int i;
21     scanf("%d", &t);
22     while (t--)
23     {
24         scanf("%s", &cstr);
25         int len = strlen(cstr); 
26         if (len < 3)
27         {
28             printf("0\n");
29             continue;
30         }
31         x = y = 0;
32         area = 0;
33 
34         for (i = 0; i < len-1; i++)
35         {
36             tx = x + dx[cstr[i]-'0'];
37             ty = y + dy[cstr[i]-'0'];
38             area += tx*y - ty*x;
39             x = tx;
40             y = ty;
41         }
42         if (area < 0) area = -area;
43         if (area % 2 == 0)
44             printf("%I64d\n",area/2); 
45         else
46             printf("%I64d.5\n",area/2); 
47     }
48     return 0;
49 }

 

转载于:https://www.cnblogs.com/lv-2012/archive/2012/12/01/2797264.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值