Mutable demo
polygon.h
Go to the documentation of this file.
1 #ifndef POLYGON_H_INCLUDED
2 #define POLYGON_H_INCLUDED
3 
4 #include <iostream>
5 #include <vector>
6 #include <cmath>
7 
10 class Point2D
11 {
12  public:
16  Point2D() : _x(0.0f), _y(0.0f) {}
17 
23  Point2D(float x, float y) : _x(x), _y(y) {}
24 
27  float GetX() const {return _x;}
28 
31  float GetY() const {return _y;}
32 
33 private:
34  float _x;
35  float _y;
36 };
37 
44 std::ostream& operator<<(std::ostream& s, const Point2D& rhs);
45 
52 // float dist(const Point2D& a, const Point2D& b);
53 inline float dist(const Point2D& a, const Point2D& b)
54 {
55  return std::sqrt( std::pow(a.GetX()-b.GetX(),2) + std::pow(a.GetY()-b.GetY(),2) );
56 }
57 
58 //------------------------------------------
59 
61 {
62  public:
63  Polygon_old(int n);
64  void append(const Point2D& a) { _v.push_back(a); }
65  int number() const { return _v.size(); }
66  float perimeter() const;
67  bool operator<(const Polygon_old& rhs) const { return perimeter() < rhs.perimeter(); }
68 
69  private:
70  std::vector<Point2D> _v;
71 };
72 
73 //------------------------------------------
77 class Polygon
78 {
79  public:
85  Polygon(int n);
86 
91  void append(const Point2D& a) { _v.push_back(a); _peri=-1.0f; }
92 
97  int number() const { return _v.size(); }
98 
104  float perimeter() const;
105 
111  bool operator<(const Polygon& rhs) const { return perimeter() < rhs.perimeter(); }
112 
113  private:
114  std::vector<Point2D> _v;
115  mutable float _peri;
116 };
117 
118 
119 
120 
121 
122 
123 #endif // POLYGON_H_INCLUDED
Point2D::GetY
float GetY() const
Getter.
Definition: polygon.h:31
Polygon_old
Definition: polygon.h:60
Polygon_old::number
int number() const
Definition: polygon.h:65
Polygon::append
void append(const Point2D &a)
Adds a vertex to the end of the polygon traverse.
Definition: polygon.h:91
Point2D
Class containing a point in 2D.
Definition: polygon.h:10
Point2D::GetX
float GetX() const
Getter.
Definition: polygon.h:27
Polygon_old::append
void append(const Point2D &a)
Definition: polygon.h:64
Polygon::operator<
bool operator<(const Polygon &rhs) const
Less operator regarding the perimeter.
Definition: polygon.h:111
Point2D::Point2D
Point2D(float x, float y)
Constructor.
Definition: polygon.h:23
Polygon_old::perimeter
float perimeter() const
Definition: polygon.cpp:31
Polygon::Polygon
Polygon(int n)
Constructs a regular polygon with vertices on the unit circle.
Definition: polygon.cpp:42
operator<<
std::ostream & operator<<(std::ostream &s, const Point2D &rhs)
Output operator for class Point2D.
Polygon
Contains the description of a polygon, now with mutable. The traverse is stored.
Definition: polygon.h:77
Polygon_old::operator<
bool operator<(const Polygon_old &rhs) const
Definition: polygon.h:67
Polygon::number
int number() const
Number of vertices in polygon.
Definition: polygon.h:97
Polygon_old::Polygon_old
Polygon_old(int n)
Definition: polygon.cpp:20
dist
float dist(const Point2D &a, const Point2D &b)
Calculates the Euclidian distance between two points in 2D.
Definition: polygon.h:53
Polygon::perimeter
float perimeter() const
Computes the perimeter of the closed polygon.
Definition: polygon.cpp:53
Point2D::Point2D
Point2D()
Constructor without parameters. Defines the point to the origin (0.0).
Definition: polygon.h:16