Mutable demo
polygon.cpp
Go to the documentation of this file.
1 #include "polygon.h"
2 
3 #include <iostream>
4 #include <vector>
5 #include <cmath>
6 using namespace std;
7 
8 ostream& operator<<(ostream& s, const Point2D& rhs)
9 {
10  s << "(" << rhs.GetX() << "," << rhs.GetY() <<")";
11  return s;
12 }
13 
14 //float dist(const Point2D& a, const Point2D& b)
15 //{
16 // return sqrt( pow(a.GetX()-b.GetX(),2) + pow(a.GetY()-b.GetY(),2) );
17 //}
18 
19 //---------------------------------------------------------------------------------
21  : _v(n)
22 {
23  for (unsigned int k=0; k<_v.size(); ++k)
24  {
25  _v.at(k) = Point2D( cos(k*2*M_PI/n), sin(k*2*M_PI/n) );
26  }
27 
28 // copy(_v.begin(),_v.end(), ostream_iterator<Point2D>(cout," "));
29 }
30 
32 {
33  float sum=dist( _v.front(),_v.back() ); // geschlossener Polygonzug
34  for (unsigned int k=1; k<_v.size(); ++k)
35  {
36  sum += dist( _v[k], _v[k-1] );
37  }
38  return sum;
39 }
40 
41 //---------------------------------------------------------------------------------
43  : _v(n), _peri(-1.0f)
44 {
45  for (unsigned int k=0; k<_v.size(); ++k)
46  {
47  _v.at(k) = Point2D( cos(k*2*M_PI/n), sin(k*2*M_PI/n) );
48  }
49 
50 // copy(_v.begin(),_v.end(), ostream_iterator<Point2D>(cout," "));
51 }
52 
53 float Polygon::perimeter() const
54 {
55  if ( _peri<0.0f )
56  {
57  _peri=dist( _v.front(),_v.back() ); // geschlossener Polygonzug
58  for (unsigned int k=1; k<_v.size(); ++k)
59  {
60  _peri += dist( _v[k], _v[k-1] );
61  }
62  }
63 
64  return _peri;
65 }
Class containing a point in 2D.
Definition: polygon.h:10
float GetX() const
Getter.
Definition: polygon.h:26
float GetY() const
Getter.
Definition: polygon.h:30
float perimeter() const
Definition: polygon.cpp:31
Polygon_old(int n)
Definition: polygon.cpp:20
Polygon(int n)
Constructs a regular polygon with vertices on the unit circle.
Definition: polygon.cpp:42
float perimeter() const
Computes the perimeter of the closed polygon.
Definition: polygon.cpp:53
ostream & operator<<(ostream &s, const Point2D &rhs)
Definition: polygon.cpp:8
float dist(const Point2D &a, const Point2D &b)
Calculates the Euclidian distance between two points in 2D.
Definition: polygon.h:52