Untitled
unknown
plain_text
a year ago
6.0 kB
8
Indexable
drawing.h
#pragma once
#include "shape.h"
#include <vector>
using namespace std;
class Drawing
{
vector<Shape *> shapes;
public:
Drawing();
~Drawing();
void AddShape(Shape *shape);
};
main.cpp
#include "point.h"
#include "polygon.h"
#include <iostream>
using namespace std;
int main()
{
Point pt1(100, 100);
Point pt2(200, 200);
Point pt3(300, 300);
Point pt4(150, 150);
Polygon poly1(&pt1, &pt2, &pt3);
poly1.ShowPolygon();
pt4 = poly1[2];
cout << pt4 << endl;
}
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);
}
point.h
#pragma once
#include "shape.h"
#include <iostream>
using namespace std;
class Point : public Shape
{
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;
};
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);
}
}
polygon.h
#pragma once
#include "point.h"
#include "shape.h"
#include <vector>
using namespace std;
class Polygon : public Shape
{
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;
};
shape.h
#pragma once
struct Shape {
virtual void Move(int x, int y) = 0;
virtual void MoveX(int x) = 0;
virtual void MoveY(int y) = 0;
virtual void Rotate(int angle) = 0;
virtual void Rotate3D(int x, int y, int z) = 0;
};Editor is loading...
Leave a Comment