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

mac2022-06-30  33

问题描述: 多边形面积问题(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

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)