Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.16 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct Rectangle {
  4.  
  5. private:
  6.  
  7.     float x, y;
  8.     float width;
  9.     float height;
  10.  
  11. public:
  12.  
  13.     Rectangle() = default;
  14.     ~Rectangle() = default;
  15.  
  16.     Rectangle(const Rectangle & rectangle) = default;
  17.  
  18.     Rectangle(float width, float height) : x(0), y(0), width(width), height(height) {}
  19.  
  20.     Rectangle(float x, float y, float width, float height) : x(x), y(y), width(width), height(height) {}
  21.  
  22.     // Площадь прямоугольника
  23.     float square() const {
  24.         return width * height;
  25.     }
  26.  
  27.     bool isLine() const {
  28.         return (height * width == 0) && (height > 0 || width > 0);
  29.     }
  30.  
  31.     /*
  32.      * Пересеченеи прямоугольников
  33.      *
  34.      * При отсутствии пересечения
  35.      * rec1.intersection(rec2).square() == 0
  36.      */
  37.     Rectangle intersection(const Rectangle & rec2);
  38.  
  39.     Rectangle& operator=(const Rectangle & rectangle);
  40.  
  41.     // Сравнвение только ширины и длины
  42.     bool operator==(Rectangle & rec) const;
  43.     bool operator!=(Rectangle & rec) const;
  44.  
  45.     float getX() const {
  46.         return x;
  47.     }
  48.  
  49.     float getY() const {
  50.         return y;
  51.     }
  52.  
  53.     float getWidth() const {
  54.         return width;
  55.     }
  56.  
  57.     float getHeight() const {
  58.         return height;
  59.     }
  60.  
  61. };
  62.  
  63. //Rectangle::Rectangle(const Rectangle &rectangle) {
  64. //    x = rectangle.x;
  65. //    y = rectangle.y;
  66. //    width = rectangle.width;
  67. //    height = rectangle.height;
  68. //}
  69.  
  70. bool Rectangle::operator==(Rectangle & rec) const {
  71.     return width == rec.width && height == rec.height;
  72. }
  73.  
  74. bool Rectangle::operator!=(Rectangle & rec) const {
  75.     return this != &rec;
  76. }
  77.  
  78. Rectangle Rectangle::intersection(const Rectangle & rec2) {
  79.     float leftX   = std::max( x, rec2.x );
  80.     float rightX  = std::min( x + width, rec2.x + rec2.width );
  81.     float topY    = std::max( y, rec2.y );
  82.     float bottomY = std::min( y + height, rec2.y + rec2.height );
  83.     if ( leftX < rightX && topY < bottomY ) {
  84.         return Rectangle( leftX, topY, rightX - leftX, bottomY - topY );
  85.     } else {
  86.         return Rectangle( leftX, topY, 0, 0 );
  87.     }
  88. }
  89.  
  90. std::ostream &operator<<(std::ostream &os, const Rectangle & rectangle) {
  91.     os << "x: " << rectangle.getX() << " y: " << rectangle.getY() << " width: " << rectangle.getWidth() << " height: " << rectangle.getHeight();
  92.     return os;
  93. }
  94.  
  95. std::istream &operator>>(std::istream &is, Rectangle & rectangle) {
  96.     float x, y, width, height;
  97.     std::cin >> x;
  98.     std::cin >> y;
  99.     std::cin >> width;
  100.     std::cin >> height;
  101.     rectangle = Rectangle(x, y, width, height);
  102.     return is;
  103. }
  104.  
  105. Rectangle& Rectangle::operator=(const Rectangle & rectangle) {
  106.     if (this == &rectangle) {
  107.         return *this;
  108.     }
  109.     x = rectangle.x;
  110.     y = rectangle.y;
  111.     width = rectangle.width;
  112.     height = rectangle.height;
  113.     return *this;
  114. }
  115.  
  116.  
  117. int main() {
  118.     Rectangle rec1, rec2;
  119.     std::cin >> rec1;
  120.     std::cin >> rec2;
  121.     rec1 = rec2;
  122.     std::cout << rec1 << std::endl;
  123.     std::cout << rec2 << std::endl;
  124. //    std::cout << rec1.intersection(rec2).square() << std::endl;
  125.     return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement