Polygon.h:
#ifndef POLYGON_H
#define POLYGON_H
#include "Shape.h"
#include <vector>
class Polygon : public Shape
{
private:
vector<Point> m_Vertices; // vector of Points
public:
// Constructors
Polygon();
Polygon(const Point *Vertices, int NumVertices);
// Destructor, required because of virtual
virtual ~Polygon();
// Get and set the private data
void SetVertices(const Point *vertices, int NumVertices);
const Point *GetVertices(void) const;
int GetNumVertices(void) const;
// Methods from Shape to specialize
virtual double Area(void) const;
virtual void Draw(void) const;
virtual void Move(const Point &position);
virtual void Rotate(double angle);
virtual void Scale(double factor);
// To print out a Polygon
friend ostream & operator<<(ostream & os, const Polygon &polygon);
};
#endif
Polygon.cpp:
include "Polygon.h"
Polygon::Polygon()
{
}
Polygon::~Polygon()
{
}
Polygon::Polygon(const Point *Vertices, int NumVertices)
{
m_Vertices.resize(NumVertices);
for (int i = 0; i < m_Vertices.size(); i++)
m_Vertices[i] = Vertices[i];
}
const Point *Polygon::GetVertices(void) const
{
static Point *vertArray = NULL;
if (vertArray == NULL)
{
vertArray = new Point[m_Vertices.size()];
for (int i = 0; i < m_Vertices.size(); i++)
vertArray[i] = m_Vertices[i];
}
return vertArray;
}
int Polygon::GetNumVertices(void) const
{
return m_Vertices.size();
}
void Polygon::SetVertices(const Point vertices[], int NumVertices)
{
m_Vertices.clear();
m_Vertices.resize(NumVertices);
for (int i = 0; i < NumVertices; i++)
m_Vertices[i] = vertices[i];
}
double Polygon::Area(void) const
{
return 0.0;
}
void Polygon::Draw(void) const
{
cout << "Drawing the polygon: " << *this << endl;
}
void Polygon::Move(const Point &position)
{
for (int i = 0; i < m_Vertices.size(); i++)
m_Vertices[i] = m_Vertices[i] + position;
}
void Polygon::Rotate(double angle)
{
for (int i = 0; i < m_Vertices.size(); i++)
m_Vertices[i] = m_Vertices[i] % angle;
}
void Polygon::Scale(double factor)
{
for (int i = 0; i < m_Vertices.size(); i++)
m_Vertices[i] = m_Vertices[i] * factor;
}
ostream & operator<<(ostream & os, const Polygon &polygon)
{
os << "NumVertices: " << polygon.m_Vertices.size();
for (int i = 0; i < polygon.m_Vertices.size(); i++)
os << ", " << polygon.m_Vertices[i];
return os;
}