area.h
Go to the documentation of this file.00001 #ifndef VVELIB_GEOMETRY_AREA_H
00002 #define VVELIB_GEOMETRY_AREA_H
00003
00010 #include <config.h>
00011 #include <geometry/geometry.h>
00012 #include <vector>
00013
00014 namespace geometry
00015 {
00019 template <size_t N>
00020 double triangleArea(const util::Vector<N,double>& a, const util::Vector<N,double>& b, const util::Vector<N,double>& c)
00021 {
00022 return util::norm((a - b)^(a - c))/2.0;
00023 }
00024
00028 template <size_t N>
00029 double polygonArea( const std::vector<util::Vector<N,double> >& polygon )
00030 {
00031 size_t k = polygon.size(), h, l, i;
00032 typename util::CrossProductType<N,double>::type surface = 0;
00033 if(polygon.empty())
00034 return 0;
00035 h = (k-1)/2;
00036 if(k%2)
00037 l = 0;
00038 else
00039 l = k-1;
00040 for(i = 1 ; i < h ; i++)
00041 surface += (polygon[2*i] - polygon[0]) ^ (polygon[2*i+1] - polygon[2*i-1]);
00042 surface += (polygon[2*h] - polygon[0]) ^ (polygon[l] - polygon[2*h-1]);
00043 surface /= 2;
00044 return util::norm(surface);
00045 }
00046
00050 Point2d centroid( const std::vector<Point2d>& polygon);
00051
00052 }
00053 #endif // VVELIB_GEOMETRY_AREA_H
00054