Untitled

 avatar
unknown
plain_text
a year ago
8.4 kB
4
Indexable
drawing.cpp



#include "drawing.h"
#include "point.h"
#include "polygon.h"
#include <iostream>
#include "shape.h"

using namespace std;

Drawing::Drawing()
{

}

Drawing::~Drawing()
{
    for (auto val : this->shapes)
        delete val;
}

void Drawing::AddPoint(Point *point)
{
    this->shapes.push_back(new Point(*point));

}

void Drawing::AddPolygon(Polygon *polygon)
{
    this->shapes.push_back(new Polygon(*polygon));
}

void Drawing::ShowDrawing(void)
{
    for (auto val: this->shapes)
    {
        val->ShowShape();
        cout << endl;
    }
}

void Drawing::MoveByX(int x)
{
    for (auto shape : this->shapes)
    {
        IMove *ptr = dynamic_cast<IMove *>(shape);
        ptr->MoveX(x);
    }
}
void Drawing::MoveByXY(int x, int y)
{
    for (auto shape : this->shapes)
    {
        IMove *ptr = dynamic_cast<IMove *>(shape);
        ptr->Move(x, y);
    }
}
void Drawing::MoveByY(int y)
{
    for (auto shape : this->shapes)
    {
        IMove *ptr = dynamic_cast<IMove *>(shape);
        ptr->MoveY(y);
    }
}

Shape &Drawing::operator[](int index)
{
    return *this->shapes[index];
}

















drawing.h



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

class Point;
class Polygon;

class Drawing
{
    vector<Shape *> shapes;
public:
    Drawing();
    ~Drawing();

    Shape &operator[](int index);
    
    void AddPoint(Point *point);
    void AddPolygon(Polygon *polygon);
    void ShowDrawing(void);

    void MoveByX(int x);
    void MoveByXY(int x, int y);
    void MoveByY(int y);
};











main.cpp




#include "point.h"
#include "polygon.h"
#include "drawing.h"
#include "shape.h"

#include <iostream>
using namespace std;

int main()
{
    Point pt1(100, 100);
    Point pt2(200, 200);
    Point pt3(300, 300);

    Polygon poly1(&pt1, &pt2, &pt3);

    Drawing drawing;

    drawing.AddPoint(&pt1);
    drawing.AddPoint(&pt2);
    drawing.AddPoint(&pt3);

    drawing.AddPolygon(&poly1);
 
    // Now perform operation on the object
    // Get a shape from the drawing
    Shape &myShape = drawing[2];
    // Move that Shape
    IMove *ptr = dynamic_cast<IMove *> (&myShape);
    ptr->Move(10, 30);
    IRotate *sPtr = dynamic_cast<IRotate *>(ptr);
    if (sPtr == nullptr)
    {
        cout << "IROTATE Interface is not implemented" << endl;
    }
    else 
    {
        cout << "Rotating SHAPE: ";
        sPtr->Rotate(100);
        cout << endl;
    }
    drawing.ShowDrawing();
}









point.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;
}


















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;
};


















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 << ", ";
}





















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;
};













shape.h


#pragma once
#include <iostream>

struct IMove {
    virtual void Move(int x, int y) = 0;
    virtual void MoveX(int x) = 0;
    virtual void MoveY(int y) = 0;
};

struct IRotate {
    virtual void Rotate(int angle) = 0;
    virtual void Rotate3D(int x, int y, int z) = 0;
};

struct Shape 
{
    virtual void ShowShape(void) = 0;
};
Editor is loading...
Leave a Comment