-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpolygon.cpp
41 lines (36 loc) · 1.06 KB
/
polygon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# include <numeric>
# include <cmath>
# include "clustr.h"
using namespace Clustr;
//using std::tr1::sqrt;
void Ring::push_back (const Point &p2) {
if (!is_empty()) {
Point p1 = vertex(size()-1);
coord_type fx = scale_x((p1.y() + p2.y())/2.0),
dx = p2.x() - p1.x(),
dy = p2.y() - p1.y();
perimeter_ += sqrt(dx*dx*fx*fx + dy*dy);
area_ += fx*fx*(p1.x() * p2.y() - p2.x() * p1.y()) / 2;
}
this->Ring_base::push_back(p2);
}
void Polygon::push_back (Ring &ring) {
if ((empty() && !ring.is_ccw()) ||
(!empty() && ring.is_ccw()))
ring.reverse_orientation();
this->Polygon_base::push_back(ring);
}
coord_type Polygon::area (void) {
coord_type total = 0;
for (Polygon::iterator ring = begin(); ring != end(); ring++) {
total += ring->area();
}
return total;
}
coord_type Polygon::perimeter (void) {
coord_type total = 0;
for (Polygon::iterator ring = begin(); ring != end(); ring++) {
total += ring->perimeter();
}
return total;
}