首页 > 其他 > 详细

向量叉乘求多变形面积

时间:2014-08-22 12:20:26      阅读:287      评论:0      收藏:0      [点我收藏+]
#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;
}

 

向量叉乘求多变形面积,布布扣,bubuko.com

向量叉乘求多变形面积

原文:http://www.cnblogs.com/sky-view/p/3928772.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!