Skip to content

Instantly share code, notes, and snippets.

@danong
Created January 24, 2014 10:25
Show Gist options
  • Save danong/8595049 to your computer and use it in GitHub Desktop.
Save danong/8595049 to your computer and use it in GitHub Desktop.
Polygon Class
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp \
polygon.cpp
HEADERS += \
vertex.h \
polygon.h
QMAKE_CXXFLAGS += -std=c++0x
#ifndef POLYGON_H
#define POLYGON_H
#include <vector>
#include "vertex.h"
class polygon
{
public:
// pre: three or more distinct vertices
// post: new polygon created that contains given vertices
polygon();
// pre: polygon p
// post: new polygon created having the same vertices as p
polygon(const polygon & other);
// pre: a polygon
// post: returns how many vertices in polygon p
size_t size() const;
// pre: polygon p, i
// post: returns veretex i in polygon p
vertex operator[](size_t i);
// pre: polygon p, vertex iv
// post: add vertex iv to polygon p
void add(vertex iv);
private:
std::vector<vertex> v;
// invariants: v0, v1, v2 are three distinct points in the plane
// invariants: p is a polygon constructed from three or more vertices (stored in a vector)
friend inline std::ostream & operator << (std::ostream & os, polygon & p);
friend inline std::istream& operator >> (std::istream & ins, polygon & p);
};
inline std::ostream & operator << (std::ostream & os, polygon & p)
{
for (auto i: p.v)
{
cout << i;
}
return os;
}
inline std::istream & operator >> (std::istream & is, polygon & p)
{
double x, y;
is >> x >> y;
p.add (vertex(x,y));
return is;
}
#endif // POLYGON_H
#include "polygon.h"
using namespace std;
polygon::polygon()
{
}
polygon::polygon(const polygon & other)
{
v=other.v;
}
size_t polygon::size() const
{
return v.size();
}
vertex polygon::operator [](size_t i)
{
return v[i];
}
void polygon::add(vertex iv)
{
v.push_back(iv);
}
#ifndef POLYGON_H
#define POLYGON_H
#include <vector>
#include "vertex.h"
class polygon
{
public:
// pre: three or more distinct vertices
// post: new polygon created that contains given vertices
polygon();
// pre: polygon p
// post: new polygon created having the same vertices as p
polygon(const polygon & other);
// pre: a polygon
// post: returns how many vertices in polygon p
size_t size() const;
// pre: polygon p, i
// post: returns veretex i in polygon p
vertex operator[](size_t i);
// pre: polygon p, vertex iv
// post: add vertex iv to polygon p
void add(vertex iv);
private:
std::vector<vertex> v;
// invariants: v0, v1, v2 are three distinct points in the plane
// invariants: p is a polygon constructed from three or more vertices (stored in a vector)
friend inline std::ostream & operator << (std::ostream & os, polygon & p);
friend inline std::istream& operator >> (std::istream & ins, polygon & p);
};
inline std::ostream & operator << (std::ostream & os, polygon & p)
{
for (auto i: p.v)
{
cout << i;
}
return os;
}
inline std::istream & operator >> (std::istream & is, polygon & p)
{
double x, y;
is >> x >> y;
p.add (vertex(x,y));
return is;
}
#endif // POLYGON_H
#ifndef POLYGON_H
#define POLYGON_H
#include <vector>
#include "vertex.h"
class polygon
{
public:
// pre: three or more distinct vertices
// post: new polygon created that contains given vertices
polygon();
// pre: polygon p
// post: new polygon created having the same vertices as p
polygon(const polygon & other);
// pre: a polygon
// post: returns how many vertices in polygon p
size_t size() const;
// pre: polygon p, i
// post: returns veretex i in polygon p
vertex operator[](size_t i);
// pre: polygon p, vertex iv
// post: add vertex iv to polygon p
void add(vertex iv);
private:
std::vector<vertex> v;
// invariants: v0, v1, v2 are three distinct points in the plane
// invariants: p is a polygon constructed from three or more vertices (stored in a vector)
friend inline std::ostream & operator << (std::ostream & os, polygon & p);
friend inline std::istream& operator >> (std::istream & ins, polygon & p);
};
inline std::ostream & operator << (std::ostream & os, polygon & p)
{
for (auto i: p.v)
{
cout << i;
}
return os;
}
inline std::istream & operator >> (std::istream & is, polygon & p)
{
double x, y;
is >> x >> y;
p.add (vertex(x,y));
return is;
}
#endif // POLYGON_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment