#include <iostream> #include <algorithm> #include <vector> #include <fstream> #include <cmath> using namespace std; struct Point { double x, y; }; //计算叉乘,平面上的点,所以向量总是沿z轴方向 double cross(const Point &A, const Point &B, const Point &C) { return (B.x - A.x) * (C.y - A.y) - (B.y - A.y) * (C.x - A.x); } //默认以(0,0)为向量起点,循环计算各三角形的面积 double polygon_area(vector<Point>& v){ double area=0; auto it=v.begin(); for(;it<v.end();++it){ area+=cross({0,0},*it,*(it+1)); } return abs(area/2.0); } int main(){ fstream fs("C:\\Documents and Settings\\Administrator\\桌面\\poly.txt"); Point tmp; vector<Point> v; while(!fs.eof()){ fs>>tmp.x>>tmp.y; v.push_back(tmp); } double area=polygon_area(v); cout<<"多边形面积为:"<<area<<endl; return 0; }
向量叉乘求多变形面积
时间: 2024-10-10 12:48:44