加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C++ 计算任意多边形面积

(2013-08-11 18:37:10)
标签:

任意多边形面积

分类: 编程算法
方法:转自红黑联盟:http://www.2cto.com/kf/201210/162799.html

题目:输入一个点列,顺次连接成一个封闭多边形,计算多边形的面积

分析:方法一,计算面积可以考虑定积分的形式,定积分有正有负,顺次求和,重复部分相互抵消,最后剩下的总面积的绝对值就是多边形的面积。

从线性积分后的结果可以容易的看出,直线段的积分实际上就是求该直线段与x轴所围成的区域的梯形的面积Int(P1, P2) = Int(k*x + b, P1.x, P2.x) = 0.5 * (P2.x - P1.x) * (P2.y + P1.y), 斜率k = (P1.y - P2.y) / (P1.x - P2.x),截距b = P1.y - k*P1.x

算法的复杂度为:O(N)N为顶点的个数。

[cpp code]
struct Point {
float x, y;
};
float LinearIntegration(const Point &p1, const Point &p2) {
return 0.5 * (p2.x - p1.x) * (p2.y + p1.y);
}
float ComputePolygonArea(const Point points[], int N) {
if (points == NULL || N <= 0) return 0.0;
float area = 0.0;
for (int i = 0; i < N - 1; ++ i) {
area += LinearIntegration(points[i], points[i + 1]);
}
area += LinearIntegration(points[N - 1], points[0]);
return area >= 0.0 ? area : -area;
}


方法二,考虑到平面上知道三角形三个顶点的坐标可以运用行列式det直接求解三角形的面积。如P1(x1,y1)P2(x2,y2)P3(x3,y3),则

S(P1, P2, P3) = det[ x1 y1 1; x2 y2 1; x3 y3 1] * 0.5 = [(x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1)] * 0.5;

可以在多边形的平面中任意的找到一个点,与多边形的每条边形成有向三角形,并顺次求取有向三角形的面积,再加和,因为是有向面积同上述定积分类似,面积有正有负可抵消重复部分,剩下的面积的绝对值即为多边形的面积。

[cpp code]
struct Point {
float x, y;
Point() {x = 0.0; y = 0.0;}
Point(float _x, float _y) {x = _x; y = _y;}
};
float ComputeTriangleArea(const Point &p1, const Point &p2, const Point &p3) {
return 0.5 * ((p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y));
}
float ComputePolygonAreaTri(const Point points[], int N) {
if (points == NULL || N <= 0) return 0.0;
Point p0(0.0, 0.0);
float area = 0.0;
for (int i = 0; i < N - 1; ++ i) {
area += ComputeTriangleArea(p0, points[i], points[i + 1]);
}
area += ComputeTriangleArea(p0, points[N - 1], points[0]);
return area >= 0.0 ? area : -area;
}

 

实例:

[cpp test code]

#include

using namespace std;

 

struct Point

 

    float x, y;  

};  

float LinearIntegration(const Point &p1, const Point &p2)

 

    return 0.5 * (p2.x - p1.x) * (p2.y + p1.y);  

 

float ComputePolygonArea(const Point points[], int length)

 

    if (points == NULL || length <= 0) return 0.0;  

    float area = 0.0;  

    for (int i = 0; i < length - 1; ++ i)

    

        area += LinearIntegration(points[i], points[i + 1]);  

    

    area += LinearIntegration(points[length - 1], points[0]);  

    return area >= 0.0 ? area : -area;  

 

 

int main()

{

    int n;

    while(cin>>n && n!=0)

    {

       Point a[n];

       for(int i=0; i

           cin>>a[i].x>>a[i].y;

       float ans = ComputePolygonArea(a,n);

       cout<<ans<<endl;

    }

    return 0;

 

}

题目:http://acm.whu.edu.cn/learn/problem/detail?problem_id=1402

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

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有