Created
January 24, 2014 10:25
-
-
Save danong/8595049 to your computer and use it in GitHub Desktop.
Polygon Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TEMPLATE = app | |
CONFIG += console | |
CONFIG -= app_bundle | |
CONFIG -= qt | |
SOURCES += main.cpp \ | |
polygon.cpp | |
HEADERS += \ | |
vertex.h \ | |
polygon.h | |
QMAKE_CXXFLAGS += -std=c++0x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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