Forms with mutable Area
polygon.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "form.h"
4 #include <cmath>
5 #include <iostream>
6 #include <vector>
7 
8 //------------------------------------------
9 
11 {
12  public:
13  Polygon_old(int n);
14  void append(const Point2D& a) { _v.push_back(a); }
15  int number() const { return static_cast<int>(_v.size()); }
16  float perimeter() const;
17  bool operator<(const Polygon_old& rhs) const { return perimeter() < rhs.perimeter(); }
18 
19  private:
20  std::vector<Point2D> _v;
21 };
22 
23 //------------------------------------------
27 class Polygon : public Form
28 {
29  public:
35  Polygon(int n);
36  Polygon(Polygon const&) = default;
37 
38 
39  std::string classname() const override
40  {
41  return "Polygon of with "+std::to_string(number())+" vertices";
42  }
43 
48  void append(const Point2D& a) { _v.push_back(a); _peri=-1.0F; _area=-1.0F; }
49 
54  int number() const { return static_cast<int>(_v.size()); }
55 
56 
62  float perimeter() const;
63 
69  float area() const override;
70 
76  bool operator<(const Polygon& rhs) const { return perimeter() < rhs.perimeter(); }
77 
78  private:
79  std::vector<Point2D> _v;
80  mutable float _peri;
81  mutable float _area;
82 };
Definition: form.h:73
Class containing a point in 2D.
Definition: form.h:12
float perimeter() const
Definition: polygon.cpp:22
Polygon_old(int n)
Definition: polygon.cpp:11
bool operator<(const Polygon_old &rhs) const
Definition: polygon.h:17
void append(const Point2D &a)
Definition: polygon.h:14
int number() const
Definition: polygon.h:15
Contains the description of a polygon, now with mutable. The traverse is stored.
Definition: polygon.h:28
bool operator<(const Polygon &rhs) const
Less operator regarding the perimeter.
Definition: polygon.h:76
int number() const
Number of vertices in polygon.
Definition: polygon.h:54
Polygon(int n)
Constructs a regular polygon with vertices on the unit circle.
Definition: polygon.cpp:33
float perimeter() const
Computes the perimeter of the closed polygon.
Definition: polygon.cpp:44
void append(const Point2D &a)
Adds a vertex to the end of the polygon traverse.
Definition: polygon.h:48
float area() const override
Computes the area of the closed polygon.
Definition: polygon.cpp:58
Polygon(Polygon const &)=default
std::string classname() const override
Definition: polygon.h:39