Advertisement
illfate

Untitled

Mar 9th, 2019
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #include "Common.h"
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. // Этот файл сдаётся на проверку
  6. // Здесь напишите реализацию необходимых классов-потомков `IShape`
  7. class Polygon : public IShape{
  8. public:
  9. void SetPosition(Point p) override {
  10. point = move(p);
  11. }
  12. Point GetPosition() const override {
  13. return point;
  14. }
  15. void SetSize(Size s) override {
  16. size = move(s);
  17. }
  18. Size GetSize() const override {
  19. return size;
  20. }
  21. void SetTexture(shared_ptr<ITexture> texture) override {
  22. itexture = move(texture);
  23. }
  24. ITexture* GetTexture() const override {
  25. return itexture.get();
  26. }
  27.  
  28. unique_ptr<IShape> Clone() const override {
  29. return CloneFigure();
  30. }
  31.  
  32. void Draw(Image& image) const override {
  33. DrawFigure(image);
  34. }
  35.  
  36. protected:
  37. Point point;
  38. Size size;
  39. shared_ptr<ITexture> itexture;
  40. virtual void DrawFigure(Image& image) const =0;
  41. virtual unique_ptr<IShape> CloneFigure() const = 0;
  42. };
  43. class Ellipse :public Polygon {
  44. void DrawFigure(Image& image) const override {
  45. for (int y = point.y; y < point.y + size.height; y++) {
  46. for (int x = point.x; x < point.x + size.width; x++) {
  47. if (y < image.size() && x<image.begin()->size()) {//проверка, что фигура не выйдет за границу изображения
  48. if (GetTexture() && y - point.y < itexture->GetSize().height && x - point.x < itexture->GetSize().width) {
  49. if (IsPointInEllipse({ x - point.x,y - point.y }, size))
  50. image[y][x] = GetTexture()->GetImage()[y - point.y][x - point.x];
  51. }
  52. else {
  53. image[y][x] = '.';
  54. }
  55. }
  56. }
  57. }
  58. }
  59. unique_ptr<IShape> CloneFigure() const {
  60. Ellipse ellipse;
  61. ellipse.SetPosition(point);
  62. ellipse.SetSize(size);
  63. ellipse.SetTexture(itexture);
  64. return make_unique<Ellipse>(ellipse);
  65. }
  66. };
  67.  
  68.  
  69. class Rectangle : public Polygon {
  70. void DrawFigure(Image& image) const override {
  71. for (int y = point.y; y < point.y + size.height; y++) {
  72. for (int x = point.x; x < point.x + size.width; x++) {
  73. if (y < image.size() && x < image.begin()->size()) {//проверка, что фигура не выйдет за границу изображения
  74. if (GetTexture() && y - point.y < itexture->GetSize().height && x - point.x < itexture->GetSize().width) {
  75. image[y][x] = GetTexture()->GetImage()[y - point.y][x - point.x];
  76. }
  77. else {
  78. image[y][x] = '.';
  79. }
  80. }
  81. }
  82. }
  83. }
  84. unique_ptr<IShape> CloneFigure() const {
  85. Rectangle rec;
  86. rec.SetPosition(point);
  87. rec.SetSize(size);
  88. rec.SetTexture(itexture);
  89. return make_unique<Rectangle>(rec);
  90. }
  91. };
  92.  
  93. // Напишите реализацию функции
  94. unique_ptr<IShape> MakeShape(ShapeType shape_type) {
  95. if (shape_type == ShapeType::Rectangle) {
  96. return make_unique<Rectangle>();
  97. }
  98. else if (shape_type == ShapeType::Ellipse) {
  99. return make_unique<Ellipse>();
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement