C++:已知各顶点坐标求多边形面积
来源:互联网 发布:oracle数据库语法 编辑:程序博客网 时间:2024/05/21 08:39
woj上的1213和1402题,给出顶点坐标求出多边形面积。
原理:
任意多边形的面积可由任意一点与多边形上依次两点连线构成的三角形矢量面积求和得出。
分析:
由于给出的点是相对于我们的坐标原点的坐标,每个点实际上我们可以当作一个顶点相对于原点的向量,如下图所示:
P(0,0)对应的顶点向量分别为:A(x0,y0),B(x1,y1),…,G(x6,y6)
另外,
且多边形面积为:
根据上述公式可以直接求出多边形的代码从而避免了边长的复杂计算。
附上两个woj原题以及代码:
1213 area
Description
Give you a simple polygon, the border is plumb or aclinic. your task is calculate the area of the polygon.
Input
first line is n(n≤100)。following N lines contains X,Y, the vertexs show as counter-clockwise. -200≤x,y≤200。
Output
print the total area.
Sample Input
10
0 0
4 0
4 1
3 1
3 3
2 3
2 2
1 2
1 3
0 3
Sample Output
9
C++:
//// main.cpp// 1213//// Created by waple on 16/12/23.// Copyright © 2016年 waple. All rights reserved.//#include <iostream>#include <cstdio>#include <cstdlib>using namespace std;int main(int argc, const char * argv[]) { int ncase; cin >> ncase; int x,y,x0,y0,sum = 0; cin >> x >> y; x0 = x; y0 = y; while (--ncase) { int xtmp,ytmp; cin >> xtmp >> ytmp; sum += (x*ytmp-y*xtmp); x = xtmp; y = ytmp; } sum += (x*y0-y*x0); printf("%d\n",int(abs(sum)/2)); return 0;}
1402 Build Your Home
Description
Mr. Tenant is going to buy a new house. In fact, he is going to buy a piece of land and build his new house on it. In order to decide which piece of land to buy, Mr. Tenant needs a program which will give a score to each piece. Each candidate piece of land is a polygonal shape (not necessarily convex), and Mr. Tenant wonders what the best score is. Among possible scores, he considered the number of vertices, sum of angles, minimum number of required guards, and so forth. Finally, Mr. Tenant decided that the best score for a piece of land would simply be its area. Your task is to write the desired scoring program.
Input
The input file consists of multiple pieces of land. Each piece is a simple polygon (that is, a polygon which does not intersect itself). A polygon description starts with a positive integer number k, followed by k vertices, where each vertex consists of two coordinates (floating-point numbers): x and y. Naturally, the last vertex is connected by an edge to the first vertex. Note that each polygon can be ordered either clockwise or counterclockwise. The input ends with a “0” (the number zero).
Output
For each piece of land, the output should consist of exactly one line containing the score of that piece, rounded to the nearest integer number. (Halves should be rounded up, but Mr. Tenant never faced such cases.) Hint: The scoring program has to handle well degenerate cases, such as, polygons with only one or two vertices.
Sample Input
1 123.45 67.890
3 0.001 0 1.999 0 0 2
5 10 10 10 12 11 11 12 12 12.0 10.0
0
Sample Output
0
2
3
C++:
//// main.cpp// 1402//// Created by waple on 16/12/23.// Copyright © 2016年 waple. All rights reserved.//#include <iostream>#include <cstdio>#include <cmath>#include <cstdlib>using namespace std;int main(int argc, const char * argv[]) { int n; while(scanf("%d",&n) && n){ double x,y,x0,y0,sum = 0.0; cin >> x >> y; x0 = x; y0 = y; while (--n) { double xtmp,ytmp; cin >> xtmp >> ytmp; sum += (x*ytmp-y*xtmp); x = xtmp; y = ytmp; } sum += (x*y0-y*x0); printf("%d\n",int(abs(sum)/2+0.5)); } return 0;}
- C++:已知各顶点坐标求多边形面积
- 已知多边形各顶点的坐标,求多边形面积
- 已知多边形顶点坐标求面积
- 求多边形面积公式(已知顶点坐标)
- 已知多边形各顶点坐标如何计算多边形面积
- 根据各顶点坐标求多边形面积
- 求多边形面积(已知坐标)
- 已知坐标求多边形面积
- 已知任意一个多边形的各个顶点的坐标,怎么去求该多边形的面积?
- 已知多边形的顶点坐标(x,y),求多边形的面积
- 已知四边形四个顶点坐标,求面积
- hdoj 2036 已知坐标求多边形面积
- 叉乘法求已知顶点坐标的n边形面积。
- 已知顶点求多边行面积
- 已知三角形三顶点坐标,求三角形面积的表达式 找出求果园里的树的解决方案
- 已知三点坐标求三角形面积
- ZOJ1032-Area 2(已知点坐标求多边形面积,求线段上的整点数量,pick定理)
- 根据闭合的多边形坐标求面积
- NSUserDefaults 简介,使用 NSUserDefaults 存储自定义对象
- librdkafka介绍文档
- 一步步教你如何在 Visual Studio 2013 上使用 Github
- 2016年12月心情总结
- 上机题目<构造表达式>
- C++:已知各顶点坐标求多边形面积
- Fix apt-get update “the following signatures couldn’t be verified because the public key is not avai
- 网站刚建设时就能给我们带来流量的方法讲解
- 学习日记 Day1
- 手势识别
- 关于企业站点的几个建议分享
- 【重构】使用简单工厂模式重构Switch语句
- map/multimap 排序操作
- 修改数据库名称