Mutable demo
polygon.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include <vector>
5 #include <cmath>
6 
9 class Point2D
10 {
11  public:
15  Point2D() : _x(0.0f), _y(0.0f) {}
16 
22  Point2D(float x, float y) : _x(x), _y(y) {}
23 
26  float GetX() const {return _x;}
27 
30  float GetY() const {return _y;}
31 
32 private:
33  float _x;
34  float _y;
35 };
36 
43 std::ostream& operator<<(std::ostream& s, const Point2D& rhs);
44 
51 // float dist(const Point2D& a, const Point2D& b);
52 inline float dist(const Point2D& a, const Point2D& b)
53 {
54  return std::sqrt( std::pow(a.GetX()-b.GetX(),2) + std::pow(a.GetY()-b.GetY(),2) );
55 }
56 
57 //------------------------------------------
58 
60 {
61  public:
62  Polygon_old(int n);
63  void append(const Point2D& a) { _v.push_back(a); }
64  int number() const { return _v.size(); }
65  float perimeter() const;
66  bool operator<(const Polygon_old& rhs) const { return perimeter() < rhs.perimeter(); }
67 
68  private:
69  std::vector<Point2D> _v;
70 };
71 
72 //------------------------------------------
76 class Polygon
77 {
78  public:
84  Polygon(int n);
85 
90  void append(const Point2D& a) { _v.push_back(a); _peri=-1.0f; }
91 
96  int number() const { return _v.size(); }
97 
103  float perimeter() const;
104 
110  bool operator<(const Polygon& rhs) const { return perimeter() < rhs.perimeter(); }
111 
112  private:
113  std::vector<Point2D> _v;
114  mutable float _peri;
115 };
Class containing a point in 2D.
Definition: polygon.h:10
Point2D(float x, float y)
Constructor.
Definition: polygon.h:22
Point2D()
Constructor without parameters. Defines the point to the origin (0.0).
Definition: polygon.h:15
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
bool operator<(const Polygon_old &rhs) const
Definition: polygon.h:66
void append(const Point2D &a)
Definition: polygon.h:63
int number() const
Definition: polygon.h:64
Contains the description of a polygon, now with mutable. The traverse is stored.
Definition: polygon.h:77
bool operator<(const Polygon &rhs) const
Less operator regarding the perimeter.
Definition: polygon.h:110
int number() const
Number of vertices in polygon.
Definition: polygon.h:96
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
void append(const Point2D &a)
Adds a vertex to the end of the polygon traverse.
Definition: polygon.h:90
float dist(const Point2D &a, const Point2D &b)
Calculates the Euclidian distance between two points in 2D.
Definition: polygon.h:52
std::ostream & operator<<(std::ostream &s, const Point2D &rhs)
Output operator for class Point2D.