Advertisement
35657

Untitled

Feb 9th, 2023
1,294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.14 KB | None | 0 0
  1. //=======================================texture.h==========================================================================
  2.  
  3. #pragma once
  4. #include "common.h"
  5.  
  6. class Texture {
  7. public:
  8.     explicit Texture(Image image)
  9.         : image_(std::move(image)) {
  10.     }
  11.  
  12.     Size GetSize() const {
  13.         if (image_.empty()) {
  14.             return {0, 0};
  15.         }
  16.         return {int(image_[0].size()), int(image_.size())};
  17.     }
  18.  
  19.     char GetPixelColor(Point p) const {
  20.         if (int(image_.size()) <= p.y || int(image_[0].size()) <= p.x || p.x < 0 || p.y < 0) {
  21.             return '.';
  22.         }
  23.         return image_[p.y][p.x];
  24.     }
  25.  
  26. private:
  27.     Image image_;
  28. };
  29.  
  30.  
  31. //============================================shapes.h=============================================================================
  32.  
  33. #pragma once
  34. #include "texture.h"
  35.  
  36. #include <memory>
  37.  
  38. // Поддерживаемые виды фигур: прямоугольник и эллипс
  39. enum class ShapeType {RECTANGLE, ELLIPSE};
  40.  
  41. class Shape {
  42. public:
  43.     // Фигура после создания имеет нулевые координаты и размер,
  44.     // а также не имеет текстуры
  45.     explicit Shape(ShapeType type) : type_(type) {
  46.     }
  47.  
  48.     void SetPosition(Point pos) {
  49.         position_ = pos;
  50.     }
  51.  
  52.     void SetSize(Size size) {
  53.         size_ = size;
  54.     }
  55.  
  56.     void SetTexture(std::shared_ptr<Texture> texture) {
  57.         texture_ = std::move(texture);
  58.     }
  59.  
  60.     // Рисует фигуру на указанном изображении
  61.     // В зависимости от типа фигуры должен рисоваться либо эллипс, либо прямоугольник
  62.     // Пиксели фигуры, выходящие за пределы текстуры, а также в случае, когда текстура не задана,
  63.     // должны отображаться с помощью символа точка '.'
  64.     // Части фигуры, выходящие за границы объекта image, должны отбрасываться.
  65.     void Draw(Image& image) const {
  66.         Size ImageSize = GetImageSize(image);
  67.         const int start_drawing_vertically = ImageSize.height - position_.y - 1;
  68.         const int end_drawing_vertically = std::max((ImageSize.height - std::min(ImageSize.height, size_.height) - position_.y), 0);
  69.         const int start_drawing_horizontally = position_.x;
  70.         const int end_drawing_horizontally = std::min(size_.width + position_.x, ImageSize.width);
  71.         int ellipse_y = size_.height - 1;
  72.         int ellipse_x = 0;
  73.         int texture_y = texture_->GetSize().height - 1;
  74.         int texture_x = 0;
  75.         if (type_ == ShapeType::RECTANGLE) {
  76.             if (!texture_) {
  77.                 for (int i = start_drawing_vertically; i >= end_drawing_vertically; --i) {
  78.                     for (int j = start_drawing_horizontally; j < end_drawing_horizontally; ++j) {
  79.                         image[i][j] = '.';
  80.                     }
  81.                 }
  82.             } else {
  83.                 for (int i = start_drawing_vertically; i >= end_drawing_vertically; --i) {
  84.                     for (int j = start_drawing_horizontally; j < end_drawing_horizontally; ++j) {
  85.                         image[i][j] = texture_->GetPixelColor({texture_x, texture_y});
  86.                         ++texture_x;
  87.                     }
  88.                     texture_x = 0;
  89.                     --texture_y;
  90.                 }
  91.             }
  92.         }
  93.         if (type_ == ShapeType::ELLIPSE) {
  94.             if (!texture_) {
  95.                 for (int i = start_drawing_vertically; i >= end_drawing_vertically; --i) {
  96.                     for (int j = start_drawing_horizontally; j < end_drawing_horizontally; ++j) {
  97.                         if (IsPointInEllipse({ellipse_x, ellipse_y}, size_)) {
  98.                             image[i][j] = '.';
  99.                         }
  100.                         ++ellipse_x;
  101.                     }
  102.                     ellipse_x = 0;
  103.                     --ellipse_y;
  104.                 }
  105.             } else {
  106.                 for (int i = start_drawing_vertically; i >= end_drawing_vertically; --i) {
  107.                     for (int j = start_drawing_horizontally; j < end_drawing_horizontally; ++j) {
  108.                         if (IsPointInEllipse({ellipse_x, ellipse_y}, size_)) {
  109.                             image[i][j] = texture_->GetPixelColor({texture_x, texture_y});
  110.                         }
  111.                         ++ellipse_x;
  112.                         ++texture_x;
  113.                     }
  114.                     ellipse_x = 0;
  115.                     texture_x = 0;
  116.                     --ellipse_y;
  117.                     --texture_y;
  118.                 }
  119.             }
  120.         }
  121.     }
  122.    
  123. private:
  124.     ShapeType type_;
  125.     Point position_;
  126.     Size size_;
  127.     std::shared_ptr<Texture> texture_;
  128. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement