Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef POLYGON_H
- #define POLYGON_H
- #include <vector>
- #include <algorithm>
- class Point {
- public:
- Point(int x = 0, int y = 0) {
- this->x = x;
- this->y = y;
- }
- void setX(int x) {
- this->x = x;
- }
- void setY(int y) {
- this->y = y;
- }
- int getX() {
- return this->x;
- }
- int getY() {
- return this->y;
- }
- friend std::ostream &operator<<(std::ostream &o, const Point &p) {
- return o << "(" << p.x << "," << p.y << ")";
- }
- bool operator==(const Point &rhs) const {
- return x == rhs.x &&
- y == rhs.y;
- }
- bool operator!=(const Point &rhs) const {
- return !(rhs == *this);
- }
- private:
- int x;
- int y;
- };
- class Polygon {
- public:
- Polygon() {
- };
- void push(Point p) {
- points.push_back(p);
- }
- void remove(const Point &p) {
- std::vector<Point>::iterator it = std::find(points.begin(), points.end(), p);
- if (it != points.end())
- points.erase(it);
- }
- unsigned int numberSegments() {
- return points.size();
- }
- bool isValid() {
- return this->points.size() > 2;
- }
- friend std::ostream &operator<<(std::ostream &o, const Polygon &p) {
- o << "[";
- for (auto i = p.points.begin(); i != p.points.end(); ++i)
- o << *i << ((i + 1) != p.points.end() ? ", " : "");
- return o << "]";
- }
- private:
- std::vector<Point> points;
- };
- void test_polygon() {
- Polygon p;
- p.push(Point(1, 1));
- std::cout << "\nPolygon p: " << p;
- std::cout << "\np.isValid(): " << p.isValid();
- p.push(Point(3, 5));
- p.push(Point(10, 10));
- p.push(Point(9));
- std::cout << "\nPolygon p: " << p;
- std::cout << "\np.isValid(): " << p.isValid();
- std::cout << "\np.numberSegments():" << p.numberSegments();
- Point p1(3, 5);
- p.remove(p1);
- std::cout << "\nPolygon p: " << p;
- std::cout << "\np.numberSegments():" << p.numberSegments();
- }
- #endif // POLYGON_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement