Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- struct Rectangle {
- private:
- float x, y;
- float width;
- float height;
- public:
- Rectangle() = default;
- ~Rectangle() = default;
- Rectangle(const Rectangle & rectangle) = default;
- Rectangle(float width, float height) : x(0), y(0), width(width), height(height) {}
- Rectangle(float x, float y, float width, float height) : x(x), y(y), width(width), height(height) {}
- // Площадь прямоугольника
- float square() const {
- return width * height;
- }
- bool isLine() const {
- return (height * width == 0) && (height > 0 || width > 0);
- }
- /*
- * Пересеченеи прямоугольников
- *
- * При отсутствии пересечения
- * rec1.intersection(rec2).square() == 0
- */
- Rectangle intersection(const Rectangle & rec2);
- Rectangle& operator=(const Rectangle & rectangle);
- // Сравнвение только ширины и длины
- bool operator==(Rectangle & rec) const;
- bool operator!=(Rectangle & rec) const;
- float getX() const {
- return x;
- }
- float getY() const {
- return y;
- }
- float getWidth() const {
- return width;
- }
- float getHeight() const {
- return height;
- }
- };
- //Rectangle::Rectangle(const Rectangle &rectangle) {
- // x = rectangle.x;
- // y = rectangle.y;
- // width = rectangle.width;
- // height = rectangle.height;
- //}
- bool Rectangle::operator==(Rectangle & rec) const {
- return width == rec.width && height == rec.height;
- }
- bool Rectangle::operator!=(Rectangle & rec) const {
- return this != &rec;
- }
- Rectangle Rectangle::intersection(const Rectangle & rec2) {
- float leftX = std::max( x, rec2.x );
- float rightX = std::min( x + width, rec2.x + rec2.width );
- float topY = std::max( y, rec2.y );
- float bottomY = std::min( y + height, rec2.y + rec2.height );
- if ( leftX < rightX && topY < bottomY ) {
- return Rectangle( leftX, topY, rightX - leftX, bottomY - topY );
- } else {
- return Rectangle( leftX, topY, 0, 0 );
- }
- }
- std::ostream &operator<<(std::ostream &os, const Rectangle & rectangle) {
- os << "x: " << rectangle.getX() << " y: " << rectangle.getY() << " width: " << rectangle.getWidth() << " height: " << rectangle.getHeight();
- return os;
- }
- std::istream &operator>>(std::istream &is, Rectangle & rectangle) {
- float x, y, width, height;
- std::cin >> x;
- std::cin >> y;
- std::cin >> width;
- std::cin >> height;
- rectangle = Rectangle(x, y, width, height);
- return is;
- }
- Rectangle& Rectangle::operator=(const Rectangle & rectangle) {
- if (this == &rectangle) {
- return *this;
- }
- x = rectangle.x;
- y = rectangle.y;
- width = rectangle.width;
- height = rectangle.height;
- return *this;
- }
- int main() {
- Rectangle rec1, rec2;
- std::cin >> rec1;
- std::cin >> rec2;
- rec1 = rec2;
- std::cout << rec1 << std::endl;
- std::cout << rec2 << std::endl;
- // std::cout << rec1.intersection(rec2).square() << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement