Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- class Rectangle
- {
- int _x1;
- int _x2;
- int _y1;
- int _y2;
- public:
- Rectangle();
- Rectangle(int, int, int, int);
- void SetRectangle(); // ввод числа
- void ShowRectangle(); //вывод на консоль
- int getLong(); //вычисление длины
- int getShort(); //вычисление ширины
- void moveRectangle(); // перемещение
- void sizeRectangle(); // изменение размеров
- ~Rectangle();
- Rectangle Intersection( Rectangle&);
- };
- =============================================
- #include "Rectangle.h"
- #include <iostream>
- using namespace std;
- Rectangle::Rectangle() { }
- Rectangle::Rectangle(int x1, int x2, int y1, int y2)
- {
- this->_x1 = x1; this->_x2 = x2; this->_y1 = y1; this->_y2 = y2;
- }
- Rectangle::~Rectangle() { }
- void Rectangle::SetRectangle()
- {
- cout << "Введите координаты x1 и y1 для первой точки: "; cin >> _x1 >> _y1;
- cout << "Введите координаты x2 и y2 для второй точки: "; cin >> _x2 >> _y2;
- if (_x1 == _x2 || _y1 == _y2)
- {
- throw exception("Ошибка! Не удалось создать прямоугольник(Прямая линия)");
- }
- }
- void Rectangle::ShowRectangle()
- {
- cout << "{" <<this->_x1 << ";" << this->_y1 << "}" << " " << "{" << this->_x2 << ";" << this->_y2 << "}" << endl;
- }
- int Rectangle::getLong()
- {
- double longer;
- longer = sqrt(pow(this->_x2 - this->_x1, 2));
- return longer;
- }
- int Rectangle::getShort()
- {
- double shorter;
- shorter = sqrt(pow(this->_y2 - this->_y1, 2));
- return shorter;
- }
- void Rectangle::moveRectangle()
- {
- int deltax, deltay;
- cout << "Введите на какое расстояние по x передвинуть прямоугольник: "; cin >> deltax;
- cout << "Введите на какое расстояние по y передвинуть прямоугольник: "; cin >> deltay;
- this->_x1 += deltax; this->_x2 += deltax; this->_y1 += deltay; this->_y2 += deltay;
- }
- void Rectangle::sizeRectangle()
- {
- double deltax, deltay;
- cout << "Изначальная длина: " << getLong() << endl;
- cout << "Изначальная ширина: " << getShort() << endl;
- cout << "Во сколько раз увеличить длину: "; cin >> deltax;
- cout << "Во сколько раз увеличить ширину: "; cin >> deltay;
- cout << "Измененная длина: " << getLong() * deltax <<endl;
- cout << "Измененная ширина:" << getShort() * deltay << endl;
- }
- Rectangle Rectangle::Intersection(Rectangle& R2)
- {
- Rectangle R3;
- int temp;
- if (this->_x2 < this->_x1)
- {
- temp = this->_x2;
- this->_x2 = this->_x1;
- this->_x1 = temp;
- }
- if (R2._x2 < R2._x1)
- {
- temp = R2._x2;
- R2._x2 = R2._x1;
- R2._x1 = temp;
- }
- if (this->_y2 < this->_y1)
- {
- temp = this->_y2;
- this->_y2 = this->_y1;
- this->_y1 = temp;
- }
- if (R2._y2 < R2._y1)
- {
- temp = R2._y2;
- R2._y2 = R2._y1;
- R2._y1 = temp;
- }
- R3._x1 = (this->_x1 > R2._x2) ? this->_x1 : R2._x1;
- R3._y1 = (this->_y1 > R2._y2) ? this->_y1 : R2._y1;
- R3._x2 = (this->_x2 < R2._x2) ? this->_x2 : R2._x2;
- R3._y2 = (this->_y2 < R2._y2) ? this->_y2 : R2._y2;
- //return ((this->_x2) > (this->_x1) && (this->_y2) > (this->_y1));
- if ((R3._x2) > (R3._x1) && (R3._y2) > (R3._y1))
- return R3;
- else
- throw exception("Ошибка!");
- }
- ===============================================
- #include "Rectangle.h"
- #include <iostream>
- using namespace std;
- int main()
- {
- setlocale(0, "Rus");
- Rectangle R1, R2, R3;
- try
- {
- R1.SetRectangle();
- R1.ShowRectangle();
- R2.SetRectangle();
- R2.ShowRectangle();
- //R1.moveRectangle();
- //R1.ShowRectangle();
- //R1.sizeRectangle();
- R3 = R1.Intersection(R2);
- R3.ShowRectangle();
- }
- catch (const exception& ex)
- {
- cout << ex.what() << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement