日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

任意多边形面积计算

發布時間:2023/12/14 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 任意多边形面积计算 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

之前,應朋友所托,完成個四邊形面積計算程序,于是不由自主考慮來個擴展,解決任意多邊形面積的計算。

?????? 一開始想到了某定點的三角形剖分,但遇到凹凸多邊形引發的多種情況,過于復雜,放棄。

?????? 后來想到用圖形學中填充算法中的掃描線方法,切分成梯形與三角形,將交點存入活性邊表后再計算面積,感覺也較復雜,放棄。

?????? 再然后,找到個計算幾何大神O’Rourke在1998年公開的成果。

*(書名:Computational Geometry in C,第二版P20)*



1-原理介紹

?????? 上書中給出定理:

任意多邊形的面積可由任意一點與多邊形上依次兩點連線構成的三角形矢量面積求和得出。

?????? 矢量面積=三角形兩邊矢量的叉乘。

?????? 如下圖:

?

按定理,多邊形面積由P點與A-G的各頂點連接所構成的三角形矢量面積構成,假定多邊形頂點坐標順序為A-G,逆時針為正方向,則有如下結論:

PAB,PBC,PCD均為順時針,面積為負;

PDE,PEF,PFG,PGA均未逆時針,面積為正;

但無論正負,均可通過P點與頂點連線的矢量叉乘完成,叉乘結果中已包含面積的正負。

?

2-程序設計

采用C++的vector(動態數組)存儲頂點坐標。

為方便計算,直接將P點定為原點(0,0),則多邊形頂點xy坐標即為向量在xy上分量。

循環計算多邊形頂點坐標每一點與下一點之間的線段,及這兩點與P連線的矢量所圍成的三角形面積。

計算面積的函數代碼如下:

iArea=iArea+(vecPoly[iCycle].x*vecPoly[(iCycle+1) % iCount].y-vecPoly[(iCycle+1) % iCount].x*vecPoly[iCycle].y); int intAreaCalc(vector<myPoint> &vecPoly) {int iCycle,iCount,iArea;iCycle=0;iArea=0;iCount=vecPoly.size();for(iCycle=0;iCycle<iCount;iCycle++){ iArea=iArea+(vecPoly[iCycle].x*vecPoly[(iCycle+1) % iCount].y-vecPoly[(iCycle+1) % iCount].x*vecPoly[iCycle].y);}return abs(0.5*iArea); }

注意,要注意的是最后一個頂點,要與第一個頂點練成三角形,可將循環變量對頂點總數求同余,則循環過程中的最后一點+1后,自然會成為第一個頂點,上述代碼中的“% iCount”即為解決此問題。

?

完整程序,請下載工程文件。

?http://files.cnblogs.com/vbspine/sdkPoly.rar

Ps:上述程序在Win7x64,VS2008環境下編譯通過。


轉載:http://www.cnblogs.com/vbspine/archive/2013/03/28/2987818.html


?方法:轉自紅黑聯盟: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

轉載:http://blog.sina.com.cn/s/blog_a2ae2da90101ofk7.html

總結

以上是生活随笔為你收集整理的任意多边形面积计算的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 一级特黄视频 | 男人插女人视频网站 | 极品尤物一区二区 | 国产成人精品免费网站 | 日本美女动态图 | 三级av在线免费观看 | 久久永久免费 | 人体内射精一区二区三区 | 久久久久久久久久一级 | 国产精品18久久久 | av动漫天堂 | 亚洲成人第一网站 | 宅男av| 黑人添美女bbb添高潮了 | 影音先锋亚洲天堂 | 欧美三级中文字幕 | 午夜大片在线观看 | 自拍偷拍一区二区三区 | 精品国产一区二区三区久久久 | 激烈娇喘叫1v1高h糙汉 | 亚洲一区二区三区香蕉 | 国产精品美女久久久久图片 | 蜜桃9999 | 国产真实在线 | 裸体视频软件 | 色原网| 久久国产传媒 | 中文字幕第28页 | 91黄色国产 | 波多野结衣影片 | 国产在线不卡 | 亚洲国产精品激情在线观看 | 久久久久久亚洲av毛片大全 | 国产精品丝袜黑色高跟鞋的设计特点 | 亚洲精品网站在线 | 久久裸体视频 | 99久久久无码国产精品免费 | 欧美一级黄色片网站 | 91人妻一区二区三区蜜臀 | 51国产偷自视频区视频 | 国产3p露脸普通话对白 | 国产一区亚洲二区三区 | 99在线成人精品视频 | 日韩精品一区二区av | 久久人人添人人爽添人人片 | 中文字幕无码乱人伦 | 亚洲欧美日韩视频一区 | 国产成人在线一区 | 99色影院 | 久久久激情视频 | 国产日韩成人 | 欧美一级久久 | 91aaaa| 99精品视频一区二区三区 | www.久久国产| 欧美变态口味重另类 | 午夜蜜桃视频 | 国产aⅴ一区二区三区 | 久久好色 | 二区在线视频 | 久久九九国产视频 | 热99视频 | 欧美性猛交99久久久久99按摩 | 久久九九免费视频 | 国产成人精品无码免费看81 | av手机免费看 | 黄色一级片免费 | h片在线观看 | 黄视频在线观看免费 | 91精品色 | 俄罗斯毛片基地 | 久久影视大全 | 国产成人99 | 天天天av | 69福利网 | 欧美日韩精选 | 成人性生交大片免费卡看 | 91大神精品在线 | 97精品一区二区视频在线观看 | 久热精品免费视频 | 国产亚洲欧美日韩高清 | 日韩av图片 | 玖草影院| 亲嘴扒胸摸屁股激烈网站 | 天天爽天天爽夜夜爽毛片 | 老妇裸体性猛交视频 | 亚洲天堂一区二区 | 在线a视频| 婷婷丁香综合 | 日韩三级免费看 | 国产特黄毛片 | 性一交一乱一色一免费无遮挡 | 在线看国产视频 | 国产视频第一区 | 福利电影一区二区三区 | 亚洲日本精品视频 | 色播基地 | 韩国伦理片在线观看 | wwwwxxxxx日本 |