自己写的求多边形的面积~
程序代码:
#include <iostream> #include <math.h> using namespace std; typedef struct tagPoint //节点的X.Y坐标 { int iX; int iY; }T_POINT; class cTriangle { private: T_POINT * m_Point; public: cTriangle(); ~cTriangle(); void Create(T_POINT * pPoint, int iNums); //pPoint指向一个点数组, iNums就是点的个数 void GetArea(int iNums); }; cTriangle::cTriangle() { } cTriangle::~cTriangle() { delete []m_Point; } void cTriangle::Create(T_POINT * pPoint, int iNums) { m_Point = new T_POINT[iNums]; memcpy(m_Point, pPoint, sizeof(T_POINT) * iNums); } void cTriangle::GetArea(int iNums) { int iFor = iNums - 2;//得出共有多少三角形 double izhou , iFirst , iSecond , iAgain , m_Area=0; for (int i = 0; i < iFor; ++i) { iFirst = sqrt(pow((m_Point[i+1].iX - m_Point[0].iX) , 2) + pow((m_Point[i+1].iY - m_Point[0].iY) , 2)); iSecond = sqrt(pow((m_Point[i+2].iX - m_Point[i+1].iX) , 2) + pow((m_Point[i+2].iY - m_Point[i+1].iY) , 2)); iAgain = sqrt(pow((m_Point[i+2].iX - m_Point[0].iX) , 2) + pow((m_Point[i+2].iY - m_Point[0].iY) , 2)); izhou = (iFirst + iSecond + iAgain) / 2; m_Area += sqrt(izhou * (izhou - iFirst) * (izhou - iSecond) * (izhou - iAgain)); } cout << m_Area << endl; } int main() { T_POINT tPOINT[4]; int iTmp; for (int i = 0; i < sizeof(tPOINT) / sizeof(tPOINT[0]); ++i) { cout << "请输入第" << i + 1 << "个节点的横坐标:" << endl; cin >> iTmp; tPOINT[i].iX = iTmp; cout << "请输入第" << i + 1 << "个节点的纵坐标:" << endl; cin >> iTmp; tPOINT[i].iY = iTmp; } cTriangle cKey; cKey.Create(tPOINT , sizeof(tPOINT) / sizeof(tPOINT[0])); cKey.GetArea(sizeof(tPOINT) / sizeof(tPOINT[0])); return 0; }