向量叉乘求多变形面积

CODE

#include <iostream>
#include <algorithm>
#include <vector>
#include <fstream>
#include <cmath>
using namespace std;
struct Point 
{ double x, y; }; 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); } 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); } 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; }
posted @ 2014-08-22 10:34  SKY_VIEW  阅读(490)  评论(0编辑  收藏  举报