Untitled

 avatar
unknown
plain_text
a year ago
5.6 kB
7
Indexable
point.h
#pragma once
#include "shape.h"

#include <iostream>
using namespace std;

class Point : public Shape, public IMove, public IRotate
{
    int x;
    int y;
public:
    Point(int x, int y);
    ~Point();
    Point(const Point &pt);
    int getX(void);
    void setX(int x);

    int getY(void);
    void setY(int y);

    Point operator+(Point &rhs);
    Point operator+(int rhs);
    friend Point operator+(int lhs, Point &rhs);
    friend ostream &operator<<(ostream &os, Point &rhs);

    bool operator>(Point &rhs);
    bool operator>(int rhs);
    Point operator=(Point& rhs);
    Point operator++(void);
    Point operator++(int);

    void Move(int x, int y) override;
    void MoveX(int x) override;
    void MoveY(int y) override;
    void Rotate(int angle) override;
    void Rotate3D(int x, int y, int z) override;
    void ShowShape(void) override;
};
//////////////////////////////////////////////
ppoint.cpp
#include "point.h"
#include <iostream>
#include <math.h>

using namespace std;

Point::Point(int x, int y) : x(x), y(y)
{
}

Point::~Point()
{
    cout << "Destroying Point: " << *this << endl;
}

Point::Point(const Point &src)
{
    this->x = src.x;
    this->y = src.y;
}

int Point::getX(void)
{
    return this->x;
}

void Point::setX(int x)
{
    this->x = x;
}

int Point::getY(void)
{
    return this->y;
}

void Point::setY(int y)
{
    this->y = y;
}


Point Point::operator+(Point &rhs)
{
    Point pt(this->x + rhs.x, this->y + rhs.y);
    return pt;
}

Point Point::operator+(int rhs)
{
    Point pt(this->x + rhs, this->y + rhs);
    return pt;
}

Point operator+(int lhs, Point &rhs)
{
    Point pt(lhs + rhs.x, lhs + rhs.y);
    return pt;
}

bool Point::operator>(int rhs)
{
    if (this->x > rhs || (this->x == rhs && this->y > rhs))
        return true;
    return false;
}

bool Point::operator>(Point &rhs)
{
    if (this->x > rhs.x || (this->x == rhs.x && this->y > rhs.y))
        return true;
    return false;
}

Point Point::operator=(Point& rhs)
{
    this->x = rhs.x;
    this->y = rhs.y;
    return *this;
}

Point Point::operator++(void)
{
    this->x++;
    this->y++;
    return *this;
}

Point Point::operator++(int)
{
    Point pt(this->x, this->y);
    this->x++;
    this->y++;
    return pt;
}

ostream &operator<<(ostream &os, Point &rhs)
{
    os << "{x: " << rhs.x << ", y: " << rhs.y << " }";

    return os;
}


void Point::Move(int x, int y)
{
    this->x += x;
    this->y += y;
}

void Point::MoveX(int x)
{
    this->x += x;
}

void Point::MoveY(int y)
{
    this->y += y;
}

void Point::Rotate(int angle)
{
    this->x = sin(angle);
    this->y = sin(angle);
}

void Point::Rotate3D(int x, int y, int z)
{
    this->x = cos(x);
    this->y = cos(x);

}

void Point::ShowShape(void)
{
    cout << *this;
}

/////////////////////////
polygon.h

#pragma once
#include "point.h"
#include "shape.h"
#include <vector>
using namespace std;

class Polygon : public Shape, public IMove, public IRotate
{
    vector<Point *> pts;
public:
    Polygon(const Point *p1, const Point *p2, const Point *p3);
   
    Polygon(const Point &p1, const Point &p2, const Point &p3);
    Polygon(vector<Point>& pts);
    Polygon(Polygon &src);
    ~Polygon();

    void AddPoint(const Point *pt);
    void AddPoint(const Point &pt);
    Point &operator[](const int index);

    void ShowPolygon(void);


    void Move(int x, int y) override;
    void MoveX(int x) override;
    void MoveY(int y) override;
    void Rotate(int angle) override;
    void Rotate3D(int x, int y, int z) override;
    void ShowShape(void) override;
};
////////////////////////////////
polygon.cpp
#include "polygon.h"
#include <iostream>
#include <stdexcept>
using namespace std;



Polygon::Polygon(const Point *p1, const Point *p2, const Point *p3)
{
    this->pts.push_back(new Point(*p1));
    this->pts.push_back(new Point(*p2));
    this->pts.push_back(new Point(*p3));
}

Polygon::Polygon(const Point& p1, const Point& p2, const Point& p3)
{
    this->pts.push_back(new Point(p1));
    this->pts.push_back(new Point(p2));
    this->pts.push_back(new Point(p3));
}

Polygon::Polygon(vector<Point>& pts)
{
    for (Point pt : pts)
    {
        this->pts.push_back(new Point(pt));
    }
}

Polygon::Polygon(Polygon &src)
{
    this->pts.clear();
    for (Point *pt : src.pts)
    {
        this->pts.push_back(new Point(*pt));
    }
}

Polygon::~Polygon()
{
    for (auto pt: this->pts)
    {
        delete pt;
    }
}

void Polygon::ShowPolygon(void)
{
    cout << "[";
    for (auto pt : this->pts)
    {
        cout << *pt << endl;
    }
    cout << "]" << endl;
}

void Polygon::AddPoint(const Point *pt)
{
    this->pts.push_back(new Point(*pt));
}

void Polygon::AddPoint(const Point & pt)
{
    this->pts.push_back(new Point(pt));
}

Point &Polygon::operator[](const int index)
{
    if (index >= pts.size())
    {
        throw std::out_of_range("Index Out of Range");
    }

    return *pts[index];
}

void Polygon::Move(int x, int y)
{
    for (auto pt : this->pts)
    {
        pt->Move(x, y);
    }
}
void Polygon::MoveX(int x)
{
    for (auto pt : this->pts)
    {
        pt->MoveX(x);
    }
}
void Polygon::MoveY(int y)
{
    for (auto pt : this->pts)
    {
        pt->MoveY(y);
    }
}

void Polygon::Rotate(int angle)
{
    for (auto pt : this->pts)
    {
        pt->Rotate(angle);
    }
}
void Polygon::Rotate3D(int x, int y, int z)
{
    for (auto pt : this->pts)
    {
        pt->Rotate3D(x, y, z);
    }
}

void Polygon::ShowShape(void)
{
    for (auto val: this->pts)
        cout << *val << ", ";
}
Editor is loading...
Leave a Comment