實施避免三角剖分的方法以找出多邊形面積的 Slicker 演算法的 C++ 程式
這是一個 C++ 程式,用於查詢多邊形區域,使用避免三角剖分的方法查詢多邊形區域。
它假定通常的數學慣例中,正 y 向上。在正 y 向下的計算機系統中,可以做的最簡單的事情是使用“正 y 向下”座標逆時針列出頂點。兩個作用然後抵消以生成正區域。
函式和虛擬碼
演算法
Begin function Area() is used to calculate area of a polygon take the polygon p as argument. for i = 0 to p.n-1 initialize j = (i + 1) % p.n; calculate t =t+((p.p[i].b * p.p[j].b) - (p.p[j].a * p.p[i].b.)) return t/2 End
示例程式碼
#include <iostream>
using namespace std;
const int MAX = 200;
class P// to declare variables {
private:
public:
double a, b;
};
class Polygon {
private:
public:
P p[MAX];
int n;
Polygon()//take the coordinates of each point of polygon {
for (int i = 0; i < MAX; i++)
P p[i];
}
};
double Area(Polygon p)//area calculation {
double t = 0;
for (int i = 0; i < p.n; i++) {
int j = (i + 1) % p.n;
t += (p.p[i].b * p.p[j].b) - (p.p[j].a * p.p[i].b);
}
return t / 2;
}
int main(int argc, char **argv) {
Polygon p;
cout << "Enter the number of points in Polygon: ";
cin >>p.n;
cout << "Enter the coordinates of each point: ";
for (int i = 0; i < p.n; i++) {
cin >>p.p[i].a;
cin >>p.p[i].b;
}
double a = Area(p);
if (a >0)//if area>0
cout << "The Area of Polygon with " << p.n
<< " points using Slicker Algorithm is : " << a;
else
cout << "The Area of Polygon with " << p.n
<< " points using Slicker Algorithm is : " << (a * -1);
}輸出
Enter the number of points in Polygon: 6 Enter the coordinates of each point: 1 1 2 2 3 3 4 4 5 5 6 7 The Area of Polygon with 6 points using Slicker Algorithm is : 2.5
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP